Basic mysql statement

a select statement

basic grammar  

select column name 1, column name 2 //You can use fully qualified column names tables. column names

form tables

filter(where)

Grouping (group by column name 1, column name 2) // The grouping basis must be all the column names of the select (except for statistical calculations), otherwise there may be ambiguity. See below for details

Having filters the grouping, not a mission alias (alias defined in select) 

order by

limit clause

 

1) distinct keyword  

select distinct A,B,C from tables; From the tables in the table, query the three columns of ABC, distinct indicates that at least one of the three columns of ABC is different. 

2)LIMIT

select A,B,C from tables limit 5; display the first five rows

select A,B,C from tables limit 5,5; display the five rows starting from row 5, the count starts from row 0

3) order by clause, sort the index rows 

order by A,B sort by A first, then sort by B The default is ascending order

order by A desc,B is sorted in descending order by A, and then in ascending order by B, desc only modifies the column in front of it.

3) where clause

= != < <= > >= BETWEEN specifies between two values

select A,B,C from tables 

where c between 2 and 10; // for assignment columns can be indexed like this where c is NULL

 

select A,B,C from tables 

where A<2 AND C>10 ; // OR also works AND has higher priority than OR A = 1 OR A=2 AND C<5 is equivalent to A = 1 OR (B>2 AND C<5)

IN is used to specify a range of conditions

select A,B,C from tables 

where A IN(1002,1003) // IN clause can also contain other SELECT statements

order by A

4) LIKE text matching  

A like 's%e' contains any arbitrary character, that is, only lines like s....e are matched, as.....e cannot be matched, LIKE is a full match

A like 's_e' underscore means any character

5) Regular expression REGEXP (regular expression rule, written later) Substring matching

A REGEXP '1000' All columns in column A that contain the substring '1000'      

1000|2000   

[123] 1 or 2 or 3

[0-9] [az] range or [:digit:] any digit same as [0-9]            

 The 4 after [:digit:]{4} indicates the match four times, that is, any 4 digits

[:digit:]{4,} no less than 4

[:digit:]{4,6} between 4 and 6

^start of text ^[0-9\\.] matches a number or . at the start of text

$ end of text

[[: start of word

[[:>:]] end of word                    

 6) AS and concat function

concat(A, '(', B,')') output column name is A(B)

A AS E gives the column an ​​alias E for display output

7) Arithmetic calculation (+ - */)

select  procid, price, num,  price*num AS total

form tables;

where  total>2000;

order by total; output the id unit price quantity and total price information of all products with a total price greater than 2000 and sort them

8) Function

text manipulation functions

left() returns the character to the left of the string

length() the length of the string

lower() converts to lowercase

LTrim() removes spaces from the left of the string

RTrim() 

Upper()

 

select procid,Upper(procname), price, num, price*num AS total //Upper(procname) output the product name in uppercase

form tables;

where  total>2000;

order by total; 

Date and time processing functions Numerical processing functions (absolute value, trigonometric functions, etc.)

9) Statistical functions

AVG                         AVG(distinct  pro_price) 

COUNT count

MAX returns the maximum value of the specified column (number or date), if used for a text column, returns the last row in positive order (sorting first makes sense)

MIN

SUM sum of column values

select AVG(pro_price) AS avg_price returns the average price for a specific column

from tables

where pro_id =2001 ; // returns the average price of product 2001

 

select Sum(pro_price*num) AS total returns the total value of the purchased product 2001

from tables

where pro_id =2001 ;  

 10) Group by group

Each supplier may provide multiple products, check the number of products provided by each supplier

select ven_id, COUNT(*) AS num_prods // Group count rows

form tables;

group by ven_id;

select 

 

select province, countrycode, sum(popu) // country province city

from city

where countrycode = 'CHN'

group by countycode; //The original intention is to count the total population of each province in China, but here it is grouped by country, only the total population of China (where) is counted, and the grouping is not detailed enough

11) having filters each group individually

select province, countrycode, sum(popu) AS popu_of_province // country province city

from city

where countrycode = 'CHN'

group by countycode,province              

having sum(popu)>200; count cities with a total population greater than 2 million in each province of China

Two subqueries (nested)

When using subqueries, it should be ensured that the select statement and the where statement have the same column information, and subqueries can also use calculated fields and statistical functions; pay attention to the use of fully qualified names

select cust_name, email // customer name, contact information

from customers // customer information table

Where  cust_id  IN  (  select cust_id     // cust_id  客户id

                                   from orders // order image, including order number, customer id, order date, there is no actual order product information here

                                  where order_num IN ( select  order_num

                                                                      from orderItems // The detailed information representation of the order saves the product id, price, quantity and other information of the order

                                                                     where proc_id = 1002) ;

 

triple link

Table vendors ven_id (vendor id) vend_name address email

表products    proc_id    proc_name   ven_id   price

                       1            milk               1001      10.0

                       2            milk               1002       8.9

 

select vend_name, proc_name price 

from vendors ,products // inner link      

where vendors.ven_id = porducts.ven_id // Without the where clause, it will do the Cartesian product of the two tables, that is, combine any rows of the two tables, and output N*M rows

order by vend_name,proc_name;

 

select vend_name, proc_name price 

from vendors INNER JOIN products // inner join      

on vendors.ven_id = porducts.ven_id // Without the where clause, it will do the Cartesian product of the two tables, that is, combine any rows of the two tables, and output N*M rows

order by vend_name,proc_name;

 

A left OUTER JOIN B // Generate the complete set of table A, if there is a match in table B, there is a value, if there is no match, it is NULL

right OUTER JOIN

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324819109&siteId=291194637