当前位置:网站首页>Summary of SQL single table query 2020.7.27
Summary of SQL single table query 2020.7.27
2022-07-07 23:28:00 【codepig16】
1. grammar
select
Field list
from
Show list
where
List of conditions
group by
Grouping field
having
Conditions after grouping
order by
Sort
limit
Paging limit
The following operations are all in this student On the table :
2. Basic query
Query each student's math score , English grades and total scores
SELECT NAME,math,IFNULL(english,0) AS english,math+IFNULL(english,0) AS ' Total score ' FROM student;
3. Conditions of the query
How to check the math scores 60 To 100 Between students and their grades
SELECT
NAME,math
FROM
student
WHERE
math BETWEEN 60 AND 100;
4. Group query
Aggregate functions : Take a column as a whole , Do the longitudinal calculation
1.count Calculate the number of
2.max Calculate the maximum
3.min
4.sum
5.avg Calculate average
Be careful :
1. Fields to be queried after grouping : Grouping fields and aggregate functions , Other fields are meaningless
2.where and having The difference between ?
- a. where Limit before grouping , If you don't do that , They don't participate in the grouping .having Limit after grouping , If the result is not satisfied , You won't find out
- b. where You can't follow an aggregate function ,having You can judge the aggregation function
Check the average score of male and female students
SELECT sex,AVG(math+english)AS AVG FROM student GROUP BY sex;
Check the average score and number of male and female students
SELECT
sex,AVG(math+english)AS AVG,COUNT(id) AS COUNT
FROM
student
GROUP BY
sex;
Group by sex . Check the men separately 、 Average score of female students , The number of requirement : The score is below 70 Divided people , Don't participate in grouping , After grouping . There are more people than 2
SELECT
sex,AVG(math+english)AS ' average ',COUNT(id) AS ' The number of '
FROM
student
WHERE
math > 70
GROUP BY
sex
HAVING
COUNT(id)>2;
5. Sort query
grammar :order by Sort field 1 sort order 1, Sort field 2 sort order 2
sort order :
ASC: Ascending Default
DESC: Descending
Be careful : When there are multiple sorting conditions , Only when the previous condition values are the same , To judge the second condition
Rank according to math scores , If the math scores are the same, they will be ranked according to the English scores
SELECT
*
FROM
student
ORDER BY
math DESC,english DESC;
Paging query
grammar :limit Index started , Number of queries per page
The formula : Index started = ( The current page number - 1)* Number of entries per page
** Be careful limit yes mysql Dialect of **
Query the top three students in Mathematics
SELECT
*
FROM
student
ORDER BY
math DESC
LIMIT
0,3;
边栏推荐
- Entity层、DAO层、Service层、Controller层 先后顺序
- 谷歌浏览器怎么登录及开启同步功能
- 云原生正在吞噬一切,开发者该如何应对?
- How to generate unique file names
- Freelink open source call center design idea
- CAIP2021 初赛VP
- RE1 attack and defense world reverse
- USB (十七)2022-04-15
- One week learning summary of STL Standard Template Library
- Cloud native data warehouse analyticdb MySQL user manual
猜你喜欢
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
LDO voltage stabilizing chip - internal block diagram and selection parameters
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
产业共融新势能,城链科技数字峰会厦门站成功举办
三问TDM
Adults have only one main job, but they have to pay a price. I was persuaded to step back by personnel, and I cried all night
Markdown
海内外技术人们“看”音视频技术的未来
leetcode-520. Detect capital letters -js
Ros2 topic (03): the difference between ros1 and ros2 [01]
随机推荐
Solution of intelligent supply chain collaboration platform in electronic equipment industry: solve inefficiency and enable digital upgrading of industry
Adults have only one main job, but they have to pay a price. I was persuaded to step back by personnel, and I cried all night
MySQL Index Optimization Practice I
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
Experience sharing of system architecture designers in preparing for the exam: the direction of paper writing
648. 单词替换
USB(十四)2022-04-12
sql 数据库执行问题
建筑建材行业SRM供应商云协同管理平台解决方案,实现业务应用可扩展可配置
2021ICPC上海 H.Life is a Game Kruskal重构树
七月第一周
V-for traversal object
13、 System optimization
Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)
Install a new version of idea. Double click it to open it
JS get the key and value of the object
ArcGIS: two methods of attribute fusion of the same field of vector elements
包装行业智能供应链S2B2B商城解决方案:开辟电商消费新生态
Dynamic agent explanation (July 16, 2020)
ROS2专题(03):ROS1和ROS2的区别【02】