当前位置:网站首页>[转]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
边栏推荐
- 669. 修剪二叉搜索树 ●●
- 中国金刚烷行业研究与投资预测报告(2022版)
- Collapse of adjacent vertical outer margins
- 2020-10-27
- Unity connects to the database
- 【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research
- AutoCAD - workspace settings
- Sixth note
- Simple HelloWorld color change
- Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory
猜你喜欢
Stm32cubemx (8): RTC and RTC wake-up interrupt
Grail layout and double wing layout
2021-10-29
Recherche de mots pour leetcode (solution rétrospective)
Learning notes of "hands on learning in depth"
Magnifying glass effect
Establish cloth effect in 10 seconds
win10虚拟机集群优化方案
AutoCAD - command repetition, undo and redo
"Measuring curve length" of CAD dream drawing
随机推荐
Database under unity
3dsmax common commands
win下一键生成当日的时间戳文件
The next key of win generates the timestamp file of the current day
被舆论盯上的蔚来,何时再次“起高楼”?
2022/7/2 question summary
django连接数据库报错,这是什么原因
[leetcode] integer inversion [7]
Generate filled text and pictures
中国金刚烷行业研究与投资预测报告(2022版)
Autocad-- Real Time zoom
Research and forecast report on China's solution polymerized styrene butadiene rubber (SSBR) industry (2022 Edition)
Lua determines whether the current time is the time of the day
Cocos2dx Lua registers the touch event and detects whether the click coordinates are within the specified area
Panel panel of UI
Download and use of font icons
C语言杂谈1
Transport connection management of TCP
win10虚拟机集群优化方案
Out and ref functions of unity