Table of Contents
Single table query
Multiple table query
Home Database Mysql Tutorial What is the query statement of mysql database

What is the query statement of mysql database

Jan 05, 2022 am 11:39 AM
mysql database Check for phrases

Query statement: 1. "select * from table name;" can query all the data in the table; 2. "select field name from table name;" can query the data of the specified field in the table; 3. "Select distinct field name from table name" can perform deduplication query on the data in the table.

What is the query statement of mysql database

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

Single table query

1. Ordinary query

(1) Command: select * from <table name>/ /通PI<p>(2) Command: <code>select <field to be queried> from <table name>;<p><strong>2 , Deduplication query (distinct) </strong></p> <p>Command: <code>select <strong>distinct</strong> <field to be queried> from <table name> <p><strong>3. Sorting query (order by) </strong></p> <p>Ascending order: asc</p> <p>Descending order: desc</p> <p>Descending order command: <code>select &amp;lt ;Field name to be queried&gt; from <table name> order by <field name to be queried> desc<p><strong>If desc is not added, the default is ascending order</strong> </p> <p><strong>4. Group by (group by)</strong></p> <p>Command: <code>select <group by>, Sum(score) from <table name> group by <group by what><p> Suppose there is another student score table (result). Request to query a student's total score. We divided them into different groups based on their student numbers. </p> <p>Command: </p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>mysql&gt;select id, Sum(score) from result group by id;</pre><div class="contentsignin">Copy after login</div></div><h2 id="Multiple-table-query">Multiple table query</h2><p><span style="font-size: 18px;"><strong>1. Equal value query</strong></span></p><p>Now there are two Table: </p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/008b44e3868246c0f346c11aa31ad2a9-0.png" class="lazy" alt="What is the query statement of mysql database"/></p><p><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/024/008b44e3868246c0f346c11aa31ad2a9-1.png" class="lazy" alt="What is the query statement of mysql database"/></p>##Now we want to<p>query the failing grades<strong> of students younger than 20 years old. </strong></p><p> Statement: <code>select stu.id,score from stu,result where stu.id = result.id and age < 20 and score < 60;

Its query is as shown below:

What is the query statement of mysql database

It can be seen that the equivalent query efficiency is too low

2. Connection query

1. Outer connection query

(1) Left outer connection query

Assume we are still using the two tables above, and

Query the failing grades of students younger than 20 years old

We use left outer join to query, first Take out all the students whose age is less than 20 years old in the student table, and then take out all the students whose grades are less than 60 in the score table, and then match them. We will find that the efficiency is greatly improved, and we can find them by matching only four times.

As shown in the figure below:

What is the query statement of mysql database The statement is:

select a.id,score
from
(select id,age from stu where age < 20) a (过滤左表信息)
left join
(select id, score from result where score < 60) b (过滤右表信息)
on a.id = b.id;
Copy after login

The left outer join is

The filtered results of the left table must all exist. If there is filtered data in the left table and there is no match in the right table, NULL will appear in the right table;

(2) Right outer join query

select a.id,score
 from
 (select id,age from stu where age < 20) a (过滤左表信息)
 right join
 (select id, score from result where score < 60) b (过滤右表信息)
 on a.id = b.id;
Copy after login

Left outer join is

The filtered results of the left table must all exist

As shown in the figure:

What is the query statement of mysql database

We found that the filtered table There are only two matching conditions that meet the condition (red means the condition is met), but the final result is:

What is the query statement of mysql database

The unmatched data in the left table is changed to empty, and the right table is filtered out All data must exist.

(3) Full outer join query

combines left outer join and right outer join, so that the data in both the left table and the right table exists.

2. Inner join query

Only filter matching results

For example, the filtered results are as follows:

What is the query statement of mysql database

The final result is:

What is the query statement of mysql database

Only matches the results we need

The statement is:

select a.id,score
 from
 (select id,age from stu where age < 20) a (过滤左表信息)
 inner join
 (select id, score from result where score < 60) b (过滤右表信息)
 on a.id = b.id;
Copy after login
[Related recommendations:

mysql video tutorial

The above is the detailed content of What is the query statement of mysql database. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's big data structure processing skills PHP's big data structure processing skills May 08, 2024 am 10:24 AM

PHP's big data structure processing skills

How to optimize MySQL query performance in PHP? How to optimize MySQL query performance in PHP? Jun 03, 2024 pm 08:11 PM

How to optimize MySQL query performance in PHP?

How to use MySQL backup and restore in PHP? How to use MySQL backup and restore in PHP? Jun 03, 2024 pm 12:19 PM

How to use MySQL backup and restore in PHP?

How to insert data into a MySQL table using PHP? How to insert data into a MySQL table using PHP? Jun 02, 2024 pm 02:26 PM

How to insert data into a MySQL table using PHP?

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

How to fix mysql_native_password not loaded errors on MySQL 8.4

How to use MySQL stored procedures in PHP? How to use MySQL stored procedures in PHP? Jun 02, 2024 pm 02:13 PM

How to use MySQL stored procedures in PHP?

How to create a MySQL table using PHP? How to create a MySQL table using PHP? Jun 04, 2024 pm 01:57 PM

How to create a MySQL table using PHP?

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

Detailed tutorial on establishing a database connection using MySQLi in PHP

See all articles