当前位置:网站首页>MySQL common statement knowledge points
MySQL common statement knowledge points
2022-07-20 19:39:00 【Cccomedy】
introduction
Mainly record your own writing sql Use unskilled or some forgotten knowledge points when speaking , It is used to deepen your study and facilitate your review .
Query statement
1. Data De duplication
keyword :distinct
usage : When there is duplicate data in the queried field , have access to distinct Deduplication ;
2. Restrict result set
keyword :limit( Parameters 1, Parameters 2) ( Parameter must be an integer )
usage :
- If only given 1 Parameters , Indicates the maximum number of record lines returned ;
SELECT * FROM table LIMIT 5 // Before retrieval 5 Data
- If given 2 Parameters , The first parameter refers to the offset of the return record line , The second specifies the maximum number of return record rows ;
SELECT * FROM table LIMIT 5,5 // Back to page 6 To the first 10 Data
- If the second parameter is -1, Indicates from the record line of the offset to the end ;
SELECT * FROM table LIMIT 5,-1 // Back to page 6 Data to the end
3. Filter query - Query criteria
keyword : where
usage : Use it in combination with query criteria , Here's the picture :
4. Aggregate functions
- MAX: Returns the maximum value of the specified data ;
- COUNT: Returns the number of data in the specified group ;
- AVG: Returns the average value in the specified group , Null values are ignored ;
5.GROUP BY
Group according to one or more columns
GROUP BY + HAVING
add HAVING Then it mainly filters the data after grouping ;
6.ORDER BY
ORDER BY col ASC: Ascending
ORDER BY col DESC: Descending
7. League table query
- INNER JOIN( Query the common part of two tables ):
table1 INNER JOIN table2 ON table1.xx = table2.xx; - LEFT JOIN( Based on the left table )
- RIGHT JOIN( On the right side )
8.UNION
usage : Merge multiple SELECT The result set of the query statement ( The results were de duplicated )
UNION ALL( The result is no weight loss )
9.CASE
- Simple CASE
CASE Test expression
WHEN Simple expressions 1 THEN The result expression 1
WHEN Simple expressions 2 THEN The result expression 2 …
WHEN Simple expressions n THEN The result expression n
[ ELSE The result expression n+1 ]
END
example : Query the class number of students in the class table 、 Class name 、 Department number and head teacher number , And deal with the serial number as follows :
When the department number is 1 when , Show “ Department of Computer Science ”;
When the department number is 2 when , Show “ Department of software engineering ”;
When the department number is 3 when , Show “ Department of Internet of things ”.
SELECT Class number , Class name ,
CASE Department number
WHEN 1 THEN ' Department of Computer Science '
WHEN 2 THEN ' Department of software engineering '
WHEN 3 THEN ' Department of Internet of things '
END AS Department number , Head teacher No
FROM Class table
- Search for CASE
CASE
WHEN Boolean expression 1 THEN The result expression 1
WHEN Boolean expression 2 THEN The result expression 2 …
WHEN Boolean expression n THEN The result expression n
[ ELSE The result expression n+1 ]
END
The above example uses search CASE have to ,
SELECT Class number , Class name ,
CASE
WHEN Department number =1 THEN ' Department of Computer Science '
WHEN Department number =2 THEN ' Department of software engineering '
WHEN Department number =3 THEN ' Department of Internet of things '
END AS Department number , Head teacher No
FROM Class table
==== 7.19 The number is more ======================================
Date function
- DAY() / DAYOFMONTH()
Return to the day which is the day of the month (1-31) - MONTH()
Return the month of the date parameter - YEAR()
Returns the year in the date parameter
String interception
- substring_index
usage :
for example :str = www.wikibt.com
- substring_index(str,‘.’,1)
result :www - substring_index(str,‘.’,2)
result :www.wikibt - substring_index(str,‘.’,-2)
result :wikibt.com - substring_index(substring_index(str,‘.’,-2),‘.’,‘1’)
result :wikibt
usage :
if count Is an integer , Then count from left to right , The first count Separators , All contents on the left ;
if count It's a negative number , Then count from right to left , The first count Separators , Everything on the right ;
Continuous updates are not finished
边栏推荐
猜你喜欢
牛血清白蛋白/羟基磷灰石(BSA/HA)复合纳米颗粒/龙葵碱人血清蛋白纳米粒的研究
Installing MySQL in centos7
I met me | virtual digital human cultivation, facegood virtual digital human open source technology seminar
ICASSP 2022 Women Session Only in Shenzhen
快速排序代码实现
The difference between threads / processes / coroutines and the unified exception handling of multithreaded states / multithreads
首批 | Magic Data等共10家企业推进中国通信院数据标注平台产品评测
【C语言】文件的处理与操作
DOM之元素的增删改查
【开源】MagicData-RAMC :180小时中文对话式语音数据集正式发布
随机推荐
数组-sort用法(排序) 可以使用函数
开发者必读:2022年移动应用运营增长洞察白皮书
JZ2440开发板-TFT LCD实验
Welcome to ICASSP 2022 - Greetings from Magic Data
【数组及数组的常用操作】
Installing MySQL in centos7
IO流小总
UML建模工具Visio 、Rational Rose、PowerDesign
干货 | 训练AI模型找不到数据?20个精选开源社区收藏好!
P7914 [CSP-S 2021] 括号序列 题解
The difference between threads / processes / coroutines and the unified exception handling of multithreaded states / multithreads
Magic data is listed in the top 100 digital economy of Internet Weekly 2022
Array sort usage (sorting) functions can be used
ARM指令CMP详解
2021-07-05
JVM调优总结 -Xms -Xmx -Xmn -Xss
GL-HSANPs 甘草酸偶联人血清白蛋白包载白藜芦醇/大黄酸磷脂复合物血清蛋白纳米粒的制备
typedef用法及使用模板
LDR指令和LDR伪指令区别
/bin/bash^M: 坏的解释器: 没有那个文件或目录