当前位置:网站首页>MySQL architecture SQL foundation 2

MySQL architecture SQL foundation 2

2022-06-23 22:08:00 Xiao Wange

DML sentence select Use

select Independent use ( Do not cooperate with other clauses )

It is generally used to query system variables ( Parameters )

 You need to remember all the parameters 
select @@port;
select @@socket;
 Recommend the following 
show variables like '% Some parameters %'

Calling system functions

select version();
select now();
 You can also call your own defined functions 

select Use with other clauses ( Single table ) Each clause can be used separately

Other clauses are ( Writing order )

from

where

group by

having

order by

limit

select coordination from Use amount to linux Of cat Query the data of a table without any other conditions

Query the whole table data

select * from t1;
* Represents all columns , You can also write all the columns   You can also query some columns , Performance is not guaranteed   Filter conditions should be added to the table ( It is forbidden to use in production )

select coordination from + where Use It is similar to filtering matching conditions

where = > < >= <= != between and in

Check all the cities in China

 select * from city where countrycode='CHN';
from  Table name  where  On the condition that  countrycode= China's 

The world population is less than 100 The country

select * from city where population<100;
from  Table name  where  On the condition that  population Less than 100 Of 

Query the world for more than 100 Wan Shao 200 Ten thousand countries

select * from city where population  between 1000000 and 2000000;
from  Table name  where Conditions  between Greater than or equal to 1000000 and  Less than or equal to 20000000

Check all cities in China and the United States

select * from city  where countrycode in ('CHN','USA');
from  Table name   where  Conditions  in intersection  ('','') Those that meet both conditions are shown   also  not in  If the conditions are met, it will not be displayed 

and or

Query the city information of Guangdong Province, China

select * from city where countrycode='CHN' and district='guangdong';
from  Table name  where  Conditions 1 and  Conditions 2  There are two conditions that need to be hidden before they are displayed 

Inquire about Chinese or American city information

## Search for information about cities in China or the United States 
select * from world.city where countrycode='CHN' or countrycode='USA';
 This query is about all cities in China and the United States   If the index is set  in Good condition and performance  or To scan the table twice 

like Conditions Only valid for string columns

The query country code is CH At the beginning , City Information

select * from world.city where countrycode like 'CH%';
 Satisfy CH The first countries and cities will be printed out 
 From a grammatical point of view CH You can add... Before and after %  It cannot be preceded by a percent sign    The index can be followed by a percent sign   The front and back can not be used to lead the full meter scanning 

group by sentence

group by General coordination Aggregate functions use count() , sum() , avg(), max(), min(), group_concat()

Count the number of cities in each country

select countrycode,count(*) from world.city group by countrycode;
1 First from  Table data 
2  then  group by  grouping 
 And then in count  Add up 

Count the total population of each country

select countrycode,sum(population) from world.city group by  countrycode;
1 First from  Table data 
2 then  group by  grouping 
 then  sum  Population   Add up 

Count the number of cities in each country , And display the city name

select  countrycode,count(*) , group_concat(name) from world.city group by countrycode;
1 First from  Table data 
2 Then group by country code  
3 First, add up the number of cities in each country   then group_concat Similar to column conversion row   Otherwise, it doesn't conform to sql_mode standard   Request either in group by after 
 Or in an aggregate function 

Perform logical

select  countrycode,count(*) ,group_concat(name) from world.city group by countrycode;
1 The data page of the original table will be taken out 
2 according to  group by  Conditional ascending sort 
3 in the light of  group by  grouping , To repeat   become 
4 then   The data rows corresponding to each group are count( Count the lines in the group )
5 And then in group_concat  Columns are converted to rows and displayed according to , Division 

Statistics city The total number of rows in the table

select count(*) from city;
 This indicates that the aggregate function may not cooperate with group by  Use   however group by  Must be used in conjunction with aggregate functions 

having Use Be similar to where, Need to be in group by Aggregate function post filter ( Condition cannot be indexed )

The number of cities in each country is greater than 100 Show

select countrycode,count(*) from world.city group by countrycode having count(*)>100; 

order by Use Sort

The number of cities in each country is greater than 100 Display and sort

select countrycode,count(*) from world.city group by countrycode having count(*)>100 order by count(*) ;
 Write in clause order , Clause can be used alone 
order by  The default is to sort from small to large   If you want to go from big to small   You can add... After the condition desc

limit In general, it's cooperation order by It makes sense to use them together

limit There are two ways of writing

If there is 10 Row data

limit N offset H

limit 3 offset 5 Skip the former 5 Row display 6-8 Row data

limit M,N

limit 5,3 skip 5 Row display 6-8 Row data

Statistics of the world's largest population 10 Cities

select * from world.city order by population desc limit 10;
limit 10  Before display 10 individual 


select  countrycode,sum(population) from world.city group by countrycode order by sum(population);

Multi table link query

The key to writing multi table links is to find the intersection column

Inquire about Each teacher taught those lessons

1 Analyze the topic to find the required table

teacher course

2 Find the association condition

teacher.tno = course.tno

3 Write statements

select teacher.tname,course.cname from  teacher join course on teacher.tno = course.tno;

Query Zhang San Name of the course studied

1 First, find the associated table

Student list student

League tables sc

The curriculum course

2 Find out the correlation conditions

student.son

sc.son

sc.con

course.con

select student.sname,course.cname from student join sc on  student.sno = sc.sno  join course on sc.cno = course.cno where student.sname='zhang3';
 Finally, the column of the person you want to query = Who will do the filtering conditions 

Count the number of courses each student studies

1 There are traps , Two sheets are all that is needed. Student sheet and grade sheet are needed ( Because students , Learning this course will lead to achievements )

 1 select  student.sname,count(*) from student join sc on student.sno = sc.sno group by student.sno; 
 2 select student.sname,count(*) from student join sc on  student.sno = sc.sno  join course on sc.cno = course.cno group by student.sname;

Count the name of each student's learning course

1 To find a table with an association

2 Find the related columns

3

select student.sname , group_concat(course.cname) from student join sc on  student.sno = sc.sno  join course on sc.cno = course.cno group by student.sno;

Alias

select_list Alias , Table alias

 The function can input the column as you want 
select user as ' user ' ,host as ' White list ' from mysql.user;
 You can output the top column as you want 
 It will be called only when it is finally output 
 It can be used in aggregate functions because aggregate functions are used in group by  After that, I called 
 Table aliases can also be set to  from after  join after   The entire statement can call 

Subquery

common

select from ()

select from join ()

select from where ()

The subquery in brackets

First, execute the query inside the brackets When querying other than the article number

The query population is less than 100 One of the , The name of the country land area

select code ,name ,surfacearea from country where code=(select countrycode from city where population<100);

union and union all The difference between

union Will marry repeat You need to use temporary tables Sort and de duplicate the result set

原网站

版权声明
本文为[Xiao Wange]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/12/202112190934431763.html