当前位置:网站首页>[turn to] MySQL operation practice (I): Keywords & functions
[turn to] MySQL operation practice (I): Keywords & functions
2022-07-05 05:14:00 【morpheusWB】
MySQL Practical operation ( One ): keyword & function

The following is the database sqltest Three tables in , Its structure and content ( part ) as follows :

1. keyword
①EXISTS、NOT EXISTS
EXISTS keyword : When EXISTS When the conditional statement in can return the record line , Condition is true , Returns the current loop This record I got ; conversely , At present loop The record we got was discarded .
NOT EXISTS keyword :NOT EXISTS And EXISTS contrary , That is to say EXISTS When the condition has a result set returned ,loop The records arrived will be discarded , Otherwise it would be loop The records to are added to the result set .
Count the students who did not take all the exams
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 | Wang Wu | F | 17 | 0614 || 6 | Zhao Qi | F | 16 | 0615 |+--------+----------+--------+------+----------+2. function
①COUNT()
COUNT() Functions can be used in two ways :
①COUNT(*): Count the number of rows in the table , Whether a table column contains null values (NULL) Or non null ;
②COUNT(col_name): Count rows with values in a specific column , Ignore NULL value ;
③COUNT(col_order): The effect same as above , When col_order=1, Then it means that the 1 Column to count . Application scenarios : Find the number of records of each course
# 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() function : Realize grouping aggregation
GROUP_CONCAT(id ORDER BY id DESC SEPARATOR ‘_’)
③SUBSTRING_INDEX()
SUBSTRING_INDEX() function : segmentation
SUBSTRING_INDEX(str, delim, count)
str: Intercepted field
delim: Separator
count: Count ,count Being positive , Left to right , Take the first place n All of the contents to the left of the separator ;count Negative , From right to left , Take the first place n All the contents to the right of the separator
Application scenarios : Find the highest score of each subject
Implementation steps :
① Will table t_score Press lesson_id grouping ,GROUP_CONCAT(score ORDER BY score DESC SEPARATOR '_');
② utilize SUBSTRING_INDEX(t.scores, '_', 1) Function .
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 |+-----------+-----------+
————————————————
Copyright notice : This paper is about CSDN Blogger 「lulin916」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/weixin_39010770/article/details/86542676
边栏推荐
- Heap sort summary
- LeetCode之單詞搜索(回溯法求解)
- Ue4/ue5 illusory engine, material part (III), material optimization at different distances
- 2022年上半年国家教师资格证考试
- BUUCTF MISC
- 十年不用一次的JVM调用
- Create a pyGame window with a blue background
- Unity synergy
- 3dsmax scanning function point connection drawing connection line
- Vs2015 secret key
猜你喜欢

BUUCTF MISC

Autocad-- dynamic zoom

Bucket sort

Pdf to DWG in CAD

54. 螺旋矩阵 & 59. 螺旋矩阵 II ●●

【论文笔记】Multi-Goal Reinforcement Learning: Challenging Robotics Environments and Request for Research

Optimization scheme of win10 virtual machine cluster

Download and use of font icons

Chinese notes of unit particle system particle effect

Unity3d learning notes
随机推荐
Cocos2dx screen adaptation
【Leetcode】1352. 最后 K 个数的乘积
Grail layout and double wing layout
Unity card flipping effect
stm32Cubemx(8):RTC和RTC唤醒中断
Embedded database development programming (zero)
Unity synergy
Embedded database development programming (VI) -- C API
2021-10-29
Embedded database development programming (V) -- DQL
Insert sort
JVM call not used once in ten years
Unity and database
Cocos progress bar progresstimer
Detailed explanation of the ranking of the best universities
C语言杂谈1
Unity ugui source code graphic
AutoCAD - scaling
Quick sort summary
2022/7/2 question summary
