当前位置:网站首页>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
边栏推荐
- 北大、加州伯克利大学等联合| Domain-Adaptive Text Classification with Structured Knowledge from Unlabeled Data(基于未标记数据的结构化知识的领域自适应文本分类)
- Open source C # WPF control library --newbeecoder UI User Guide (II)
- Xgboost implements text classification and sklearn NLP library tfidfvectorizer
- How to use the serial port assistant in STC ISP?
- How to write test cases efficiently?
- Tencent cloud server ubuntu18 installs MySQL and logs in remotely
- How to control the quality of omics research—— Mosein
- Polar cycle graph and polar fan graph of high order histogram
- Common commands for cleaning up kubernetes cluster resources
- [proteus simulation] lcd1602+ds1307 key setting simple clock
猜你喜欢

万字长文!一文搞懂InheritedWidget 局部刷新机制

北大、加州伯克利大學等聯合| Domain-Adaptive Text Classification with Structured Knowledge from Unlabeled Data(基於未標記數據的結構化知識的領域自適應文本分類)

Sending network request in wechat applet

嵌入式开发:嵌入式基础——重启和重置的区别

How to use the serial port assistant in STC ISP?

Find my information | Apple may launch the second generation airtag. Try the Lenz technology find my solution

Installation and use of Minio

《阿里云天池大赛赛题解析》——O2O优惠卷预测

ICML2022 | 基于对比学习的离线元强化学习的鲁棒任务表示

使用 Provider 改造屎一样的代码,代码量降低了2/3!
随机推荐
嵌入式开发:嵌入式基础——重启和重置的区别
Dart series: look at me for security. The security feature in dart is null safety
Surprise! Edge computing will replace cloud computing??
Leetcode algorithm interview sprint sorting algorithm theory (32)
How to use zero to build a computer room
[同源策略 - 跨域问题]
Nanny level anti crawling teaching, JS reverse implementation of font anti crawling
高阶柱状图之极环图与极扇图
KnowDA: All-in-One Knowledge Mixture Model for Data Augmentation in Few-Shot NLP(KnowDA:用于 Few-Shot NLP 中数据增强的多合一知识混合模型)
Outlook开机自启+关闭时最小化
The 11th Blue Bridge Cup
How to improve the content quality of short video, these four elements must be achieved
[emergency] log4j has released a new version of 2.17.0. Only by thoroughly understanding the cause of the vulnerability can we respond to changes with the same method
Installation and use of Minio
How to defend the security importance of API gateway
A batch layout WAF script for extraordinary dishes
Data visualization: summer without watermelon is not summer
BenchCLAMP:评估语义分析语言模型的基准
EDI mandatory manual
Redis source code analysis -- QuickList of redis list implementation principle