当前位置:网站首页>[转]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
边栏推荐
- Count sort
- Dotween usage records ----- appendinterval, appendcallback
- 2021-10-29
- UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化
- win10虚拟机集群优化方案
- Magnifying glass effect
- Unity card flipping effect
- 3dsmax2018 common operations and some shortcut keys of editable polygons
- Research on the value of background repeat of background tiling
- Stm32cubemx (8): RTC and RTC wake-up interrupt
猜你喜欢
随机推荐
Download and use of font icons
PMP考试敏捷占比有多少?解疑
UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化
Lua wechat avatar URL
Time format conversion
How to choose a panoramic camera that suits you?
Reverse one-way linked list of interview questions
MySQL audit log Archive
中国针状焦行业发展研究与投资价值报告(2022版)
MySQL audit log archiving
Data is stored in the form of table
cocos2dx_ Lua particle system
中国艾草行业研究与投资前景预测报告(2022版)
嵌入式数据库开发编程(五)——DQL
Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
Lua determines whether the current time is the time of the day
This article is good
AutoCAD - full screen display
Page countdown
中国金刚烷行业研究与投资预测报告(2022版)










