当前位置:网站首页>[转]MySQL操作实战(一):关键字 & 函数
[转]MySQL操作实战(一):关键字 & 函数
2022-07-05 05:09:00 【morpheusWB】
MySQL操作实战(一):关键字 & 函数

以下为数据库sqltest中的三张表,其结构和内容(部分)如下:

1. 关键字
①EXISTS、NOT EXISTS
EXISTS关键字:当EXISTS里的条件语句能够返回记录行时,条件为真,返回当前loop到的这条记录;反之,当前loop到的这条记录被丢弃。
NOT EXISTS关键字:NOT EXISTS与EXISTS相反,也就是当EXISTS条件有结果集返回时,loop到的记录将被丢弃,否则将loop到的记录加入结果集。
统计没有参加所有考试的学生
mysql> SELECT * -> FROM t_stu_profile b -> WHERE NOT EXISTS( -> SELECT * -> FROM t_score a -> WHERE a.stu_id = b.stu_id) -> ;+--------+----------+--------+------+----------+| Stu_id | Stu_Name | Gender | Age | Class_id |+--------+----------+--------+------+----------+| 5 | 王五 | F | 17 | 0614 || 6 | 赵七 | F | 16 | 0615 |+--------+----------+--------+------+----------+2. 函数
①COUNT()
COUNT()函数有两种使用方式:
①COUNT(*):对表中行的数目进行计数,不管表列中包含的是空值(NULL)还是非空值;
②COUNT(col_name):对特定列中具有值的行进行计数,忽略NULL值;
③COUNT(col_order):效果同上,当col_order=1,则表明对第1列进行计数。应用场景:查找各门课程的记录数量
# COUNT(*)mysql> SELECT lesson_id, COUNT(*) AS nums -> FROM t_score -> GROUP BY lesson_id -> ;+-----------+------+| lesson_id | nums |+-----------+------+| L001 | 4 || L002 | 4 || L004 | 1 || L003 | 3 || L005 | 2 |+-----------+------+# COUNT(col_name)mysql> SELECT lesson_id, COUNT(lesson_id) AS nums -> FROM t_score -> GROUP BY lesson_id -> ;# COUNT(col_order)mysql> SELECT lesson_id, COUNT(2) AS nums -> FROM t_score -> GROUP BY lesson_id -> ;②GROUP_CONCAT()
GROUP_CONCAT()函数:实现分组聚合
GROUP_CONCAT(id ORDER BY id DESC SEPARATOR ‘_’)
③SUBSTRING_INDEX()
SUBSTRING_INDEX()函数:切分
SUBSTRING_INDEX(str, delim, count)
str:被截取字段
delim:分隔符
count:计数,count为正,从左往右数,取第n个分隔符的左边的全部内容;count为负,从右往左数,取第n个分隔符的右边的全部内容
应用场景:查找每科的最高分
实现步骤:
①将表t_score中的记录按 lesson_id 分组,GROUP_CONCAT(score ORDER BY score DESC SEPARATOR '_');
②利用SUBSTRING_INDEX(t.scores, '_', 1)函数进行切分。
mysql> SELECT t.lesson_id, SUBSTRING_INDEX(t.scores, '_', 1) AS max_score -> FROM( -> SELECT lesson_id, GROUP_CONCAT(score ORDER BY score DESC SEPARATOR '_') AS scores -> FROM t_score -> GROUP BY lesson_id) t -> GROUP BY t.lesson_id -> ;+-----------+-----------+| lesson_id | max_score |+-----------+-----------+| L001 | 100 || L002 | 91 || L003 | 86 || L004 | 75 || L005 | 98 |+-----------+-----------+
————————————————
版权声明:本文为CSDN博主「lulin916」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_39010770/article/details/86542676
边栏推荐
- UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化
- MD5 bypass
- Research and forecast report on China's solution polymerized styrene butadiene rubber (SSBR) industry (2022 Edition)
- UE fantasy engine, project structure
- mysql审计日志归档
- Pause and resume of cocos2dx Lua scenario
- Heap sort summary
- 2022/7/2 question summary
- GameObject class and transform class of unity
- Chinese notes of unit particle system particle effect
猜你喜欢

Generate filled text and pictures

AutoCAD - window zoom

Pdf to DWG in CAD

Autocad-- dynamic zoom
![[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research](/img/17/db8614b177f33ee4f67b7d65a8430f.png)
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research

AutoCAD - continuous annotation

Grail layout and double wing layout

"Measuring curve length" of CAD dream drawing

win10虚拟机集群优化方案

Research on the value of background repeat of background tiling
随机推荐
用 Jmeter 工具做个小型压力测试
Reverse one-way linked list of interview questions
Quick sort summary
Time format conversion
Optimization scheme of win10 virtual machine cluster
Transport connection management of TCP
Simple HelloWorld color change
cocos2dx_ Lua card flip
Judge the position of the monster in the role under unity3d
2022/7/1 learning summary
Magnifying glass effect
Use of snippets in vscode (code template)
AutoCAD - graphic input and output
Unity check whether the two objects have obstacles by ray
UE4/UE5 虚幻引擎,材质篇,纹理,Compression and Memory压缩和内存
UE 虚幻引擎,项目结构
3dsmax snaps to frozen objects
Unity connects to the database
A three-dimensional button
小程序直播+電商,想做新零售電商就用它吧!
