当前位置:网站首页>[转]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
边栏推荐
- Create a pyGame window with a blue background
- 669. Prune binary search tree ●●
- Research on the value of background repeat of background tiling
- Chinese notes of unit particle system particle effect
- [paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
- Research and forecast report on China's solution polymerized styrene butadiene rubber (SSBR) industry (2022 Edition)
- AutoCAD - continuous annotation
- Time format conversion
- A complete attack chain
- 十年不用一次的JVM调用
猜你喜欢

Create a pyGame window with a blue background

Embedded database development programming (V) -- DQL

Unity check whether the two objects have obstacles by ray

【Leetcode】1352. Product of the last K numbers

Establish cloth effect in 10 seconds

BUUCTF MISC

小程序直播+電商,想做新零售電商就用它吧!

Chinese notes of unit particle system particle effect

小程序直播+电商,想做新零售电商就用它吧!

2022/7/2 question summary
随机推荐
3dsmax2018 common operations and some shortcut keys of editable polygons
AutoCAD - window zoom
Kali 2018 full image download
54. Spiral matrix & 59 Spiral matrix II ●●
C # perspective following
小程序直播+電商,想做新零售電商就用它吧!
54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●
MySQL audit log archiving
Learning notes of "hands on learning in depth"
小程序直播+电商,想做新零售电商就用它吧!
Data is stored in the form of table
Unity check whether the two objects have obstacles by ray
Unity writes timetables (without UI)
Lua determines whether the current time is the time of the day
嵌入式数据库开发编程(六)——C API
Unity and database
3dsmax scanning function point connection drawing connection line
win10虚拟机集群优化方案
mysql审计日志归档
AutoCAD - stretching
