Summary of MySQL SELECT statement

Source: Internet
Author: User

Single-Table query:

Query specified record select .... where

Query with in keyword not in

Range query with between and

Select Column1,column2 from table1 where column between 3 and 14;

Query with like% matches characters of any length

Query the name of the fruit that starts with B and ends with Y

Select F_name from fruit where f_name like ' b%y ';

Underline ' _ ', can only match any one character at a time.

Query null value: is null/is NOT null

SELECT * from where c_email is null;

Multi-criteria query with and

Select f_id, f_price from fruits where s_id = ' 101 ' and F_price >= 5;

A multi-conditional query with or, the OR and in operations achieve the same effect. But in more concise

Query results are not duplicated:

Select distinct s_id from fruits;

Sort:

ORDER BY id desc (Descending) default ASC (Ascending)

If the order by is followed by more than one field, the first one is sorted, if the first compares equal, in the order of the following;

Group query:

Group BY column have < conditional expressions >

This is often used with aggregation functions with Max () min () count () Avg () sum ()

Select s_id, COUNT (*) as total from Fruits Group by s_id;

The above query the name of the fruit supplied by the ID supplier;

Select s_id, Group_concat (f_name) as names from fruit Group by s_id;

Group_concat () Displays the name of each group

Having filter group

Select s_id, Group_concat (f_name) as names from Fruits Group by s_id have count (f_name) > 1;

Using the WITH rollup in the GROUP BY clause

Add a row to show the sum of the fields

Multi-field grouping is also possible

SELECT * from Fruit Group by s_id, F_name;

Limit () Limits query results

Connection query:

Internal connection: (inner join)

Select suppliers.s_id, S_name, f_price from fruit, suppliers

where fruits.s_id = Suppliers.s_d;

Internal connection Query statement:

Select ... form fruits INNER JOIN suppliers on fruit.s_id = suppliers.s_id;

Summary of MySQL SELECT statement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.