当前位置:网站首页>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;

边栏推荐
- re1攻防世界逆向
- Solve the problem of duplicate request resource paths /o2o/shopadmin/o2o/shopadmin/getproductbyid
- Turbo introder common scripts
- FPGA基础篇目录
- ArcGIS: field assignment_ The attribute table field calculator assigns values to fields based on conditions
- USB(十六)2022-04-28
- Explain
- 做自媒体视频剪辑怎么赚钱呢?
- v-for遍历对象
- Matlab SEIR infectious disease model prediction
猜你喜欢

Matlab 信号处理【问答随笔·2】

2021ICPC上海 H.Life is a Game Kruskal重构树

B_QuRT_User_Guide(38)

Talk about the design and implementation logic of payment process

STL标准模板库(Standard Template Library)一周学习总结

Unity3D学习笔记6——GPU实例化(1)

LeeCode -- 6. Zigzag transformation

ArcGIS: two methods of attribute fusion of the same field of vector elements

Vulnerability recurrence ----- 49. Apache airflow authentication bypass (cve-2020-17526)

LM12丨Rolling Heikin Ashi二重K线滤波器
随机推荐
Explain
Two kinds of curves in embedded audio development
648. 单词替换
电子设备行业智能供应链协同平台解决方案:解决低效, 赋能产业数字化升级
Spark 离线开发框架设计与实现
Force deduction solution summary 648 word replacement
Unity3d learning notes 4 - create mesh advanced interface
648. Word replacement
In the field of software engineering, we have been doing scientific research for ten years!
Senior programmers must know and master. This article explains in detail the principle of MySQL master-slave synchronization, and recommends collecting
Turbo introder common scripts
移动端异构运算技术 - GPU OpenCL 编程(基础篇)
FPGA basics catalog
Coreseek: the second step is index building and testing
The 19th Zhejiang Provincial College Programming Contest VP record + supplementary questions
B_QuRT_User_Guide(36)
Install Fedora under RedHat
USB (十七)2022-04-15
Mysql索引优化实战一
UE4_ Use of ue5 blueprint command node (turn on / off screen response log publish full screen display)