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

边栏推荐
- 树后台数据存储(採用webmethod)[通俗易懂]
- 经纬度PLT文件格式说明
- USB (XV) 2022-04-14
- 系统设计概述
- 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
- Coreseek:第二步建索引及測试
- Description of longitude and latitude PLT file format
- 移动端异构运算技术 - GPU OpenCL 编程(基础篇)
- Dynamic agent explanation (July 16, 2020)
- sql 数据库执行问题
猜你喜欢

Inftnews | web5 vs Web3: the future is a process, not a destination

高效的S2B2C电商系统,是这样帮助电子材料企业提升应变能力的

Mysql索引优化实战一

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

高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏

Matlab-SEIR传染病模型预测

UE4_ Ue5 combined with Logitech handle (F710) use record

在软件工程领域,搞科研的这十年!

One week learning summary of STL Standard Template Library

Unity3D学习笔记6——GPU实例化(1)
随机推荐
家用电器行业渠道商协同系统解决方案:助力家电企业快速实现渠道互联网化
UE4_UE5全景相机
HDU 4747 Mex「建议收藏」
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
USB (XVII) 2022-04-15
统计电影票房排名前10的电影并存入还有一个文件
windows设置redis开启自动启动
Matlab-SEIR传染病模型预测
Adrnoid Development Series (XXV): create various types of dialog boxes using alertdialog
聊聊支付流程的设计与实现逻辑
As a new force, chenglian premium products was initially injected, and the shares of relevant listed companies rose 150% in response
[compilation principle] lexical analysis design and Implementation
ROS2专题(03):ROS1和ROS2的区别【01】
Three questions TDM
树后台数据存储(採用webmethod)[通俗易懂]
The 19th Zhejiang Provincial Collegiate Programming Contest VP记录+补题
成年人只有一份主业是要付出代价的,被人事劝退后,我哭了一整晚
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
2021ICPC上海 H.Life is a Game Kruskal重构树
海内外技术人们“看”音视频技术的未来