Database Management and High Availability Chapter 5 SQL Advanced Statements

Database Management and High Availability Chapter 5 SQL Advanced Statements

1.1: Sort by keywords

SELECT column1,column2,...FROM table_name ORDER BY column1,column2,...
ASC|DESC;
mysql> select id,name,score from accp where score>80 order by score desc;

1.2: Sort by multiple fields

mysql> select id,name,score from accp where score>80 order by score desc,id desc;
//注意每个字段都要写asc(默认)或者desc

1.3: Group the results

SELECT column_name,aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name;

1.3.1: GROUP BY grouping the same type into a group

mysq> select count(name),level from player where level>=45 group by level;
mysql> select count(name),score,age from kgc where score>=80 group by score;
+-------------+-------+-----+
| count(name) | score | age |
+-------------+-------+-----+
|           1 | 80.00 |  20 |
|           2 | 88.00 |  18 |
|           1 | 90.00 |  28 |
+-------------+-------+-----+

1.3.2: GROUP BY结合ORDER BY

mysql> select count(name),score,age from kgc where score>=80 group by score order by count(name) desc;
+-------------+-------+-----+
| count(name) | score | age |
+-------------+-------+-----+
|           2 | 88.00 |  18 |
|           1 | 90.00 |  28 |
|           1 | 80.00 |  20 |
+-------------+-------+-----+

1.4: Limit result entries

SELECT column1,column2,....FROM table_name LIMIT[offset,] number;
number 返回记录行的最大数目
[offset,] 位置偏移量,从0开始
[a,b] 索引起始值 b代表包括a-1的行开始往下b行
mysql> select id,name from kgc limit 3;  //显示前三条数据 起始位是0代表第一条数据,代表第一行
mysql> select id,name from kgc limit 2,3; 起始位2,代表第三条记录,第 3 条记录开始显示之后的 3条数据。

1.5: Set alias

SELECT column_name AS alias_name FROM table_name;
SELECT column_name(s)FROM table_name AS alias_name;
*字段的别名
mysql> select count(*) as number from player;
+--------+
| number |
+--------+
|3218    |   
+--------+
*表的别名
mysql> select a.name,h.hob_name from accp a inner join Hob h on a.hobby=h.id;
把accp表 as a, Hob表 as h 定义别名,as可以省略
from 表 表的别名

1.6: Use wildcards in combination with like

mysql> select id,name,score from accp where name like 'ss%';
// like ss开头%任意结尾
mysql> select id,name,score from accp where name like '%s%';
//like s 两边任意字符

mysql> create table bbb as select * from kgc;   复制表

1.7: Subqueries

*IN语句是用来判断某个值是否在给定的结果集中,判断是否等于集合中的某个值
mysql> select name,hobby from accp where hobby in (select id  from Hob where hob_name='云计算');
mysql> select name,hobby from accp where hobby in (select id from Hob);
+----------+-------+
| name     | hobby |
+----------+-------+
| wangwu   |     2 |
| lisi     |     1 |
| wangwu   |     3 |
| zhangsan |     2 |
+----------+-------+
//先执行()里的select语句,把结果的集合作为前面select语句的查询条件
mysql> delete from tmp where id in (select a.id from (select id from tmp  where age=20) a);
//把(select id from tmp  where age=20)的结果定义别名给a

1.8: NULL value

*空值长度为0,不占空间;NULL值的长度为NULL,占用NULL这个字符空间 NULL就好比"真空" 空值好比"空气"
//Conut计数空值会计入统计
//COUNT计数时NULL会被忽略
//空值不是NULL
mysql> select * from num where name is not null;
+----+------+
| id | name |
+----+------+
|  1 | tom  |
|  3 |      |
+----+------+
mysql> select * from num where name is null;
+----+------+
| id | name |
+----+------+
|  2 | NULL |
+----+------+

1.9: Regular expressions

1.9.1: Records starting with a specific string

mysql> select id,name,score from accp where name regexp '^li';

1.9.2: Records ending with a specific string

mysql> select id,name,score from accp where name regexp 'wu$';

1.9.3: Records containing the specified string

mysql> select id,name,score from accp where name regexp 'an';

1.9.4: Use "." to replace any character record in the string

mysql> select id,name,score from accp where name regexp 'zhang.an';   //regexp英文就是正则表达式的意思

1.9.5: Match records that contain or relation

mysql> select id,name,score from accp where name regexp 'an|si';

1.9.6: Match any number of previous characters

mysql> insert into num (id,name) values (4,'oooo'),(5,'ooooo');

1.9.7:'+' matches the preceding character at least once

mysql> select id,name from num where name regexp 'ooooo+';

1.9.8: Match any one of the specified character set

mysql> select id,name from num where name regexp '^[a-z]';

1.10: Operator

1.10.1: Arithmetic operators

mysql> select 1+2,2*3,5-4,7%2,7/2;

1.10.2: Comparison operators

---等于运算符
mysql> select 2=4,2='2','e'='e',(2+2)=(3+1),'r'=NULL;
            0 |     1 |       1 |           1 |     NULL
---不等于运算符
mysql> select 'kgc'!='bdqn',12<>13,NULL<>NULL;
|        1 |      1 |       NULL 
---大于、大于等于、小于、小于等于运算符
条件成立返回1,不成立返回0

---IS NULL、IS NOT NULL
IS NULL 判断一个值是否为 NULL,如果为 NULL 返回 1,否则返回 0。
IS NOT NULL 判断一个值是否不为 NULL,如果不为 NULL 返回 1,否则返回 0

---BETWEEN AND
BETWEEN AND 比较运算通常用于判断一个值是否落在某两个值之间.
mysql> select 4 between 2 and 6,5 between 5 and 10,'f' between 'a' and 'z';
|                 1 |                  1 |                       1 |
---LEAST、GREATEST (最小值,最大值)
---IN NOT IN    IN在对应的列表里返回1,否则返回0  NOT IN 返回值相反

1.11: Logical operators

AND && AND All 1 is 1
or || OR See 1, 1 is
not NOT! Inverted
exclusive OR XOR All zeros are zero, all 1s (non-zero) are zero, and one zero and one non-zero are 1

1.12: Bit operator-calculation of binary numbers

Operator description
& Bitwise and
~ Bitwise negation
^ Bitwise XOR
<< Bit shift left
>> Bit shift right

Regarding the AND operation and the OR operation
//The AND operation all 1 is 1
1010 10
1111 15 The
result
1010=10
//Or operation sees 1
1010
1111 The
result
1111=15
//Exclusive OR operation
1010
1111 The
result
0101=5

The left shift operator shifts to the left, the excess part is removed, and the vacant part is zero-filled. The
right shift operator shifts to the right, and the excess part is removed.
"15>>2" converts the number 15 to binary which is 1111, and shifts two bits to the right. The two bits of 11 on the right are discarded and become 11, and the left is filled with 00, and finally becomes 0011 in binary, which is 3 when converted to decimal.
1111 Shift two bits to the right
11 Fill with zeros in front
0011
0010 Shift with two bits to the left
10 Fill with zeros in the back
1000
// 5&~1 means the inversion operation of 5 and 1
0001 Inversion 1110
1110 The sum operation is all 1, then 1
0101
result
0100=4

1.13 Precedence of operators

Operators with higher levels will be calculated first. If the operators are of the same level, MySQL will calculate in order from left to right.

priority Operator priority Operator
1 8
2 ~ 9 =,<=>,>=,>,<=,<,<>,!=,IS,LIKE,REGEXP,IN
3 ^ 10 BETWEEN,CASE,WHEN,THEN,ELSE
4 *, / (DIV),% (MOD) 11 NOT
5 +,- 12 &&,AND
6 >>,<< 13
7 & 14 :=

"!" has the highest priority, and ":=" has the lowest priority.

1.14 Connect query

1.14.1 Inner join-intersection

select a.列,b.列 from a表 a(别名) inner join b表 b(别名) on a(列)=b(列);
mysql> select a.name,h.hob_name from accp a inner join Hob h on a.hobby=h.id;
+----------+--------------+
| name     | hob_name     |
+----------+--------------+
| wangwu   | 大数据       |
| lisi     | 云计算       |
| wangwu   | 人工智能     |
| zhangsan | 大数据       |
+----------+--------------+

1.4.2 Left connection

左表是主表,全显示,右表是辅表,匹配到才显示,匹配不到的NULL

select a.列,b.列 from a表 a(别名) left join b表 b(别名) on a(列)=b(列);

mysql> select a.name,h.hob_name from accp a left join Hob h on a.hobby=h.id;
+----------+--------------+
| name     | hob_name     |
+----------+--------------+
| lisi     | 云计算       |
| wangwu   | 大数据       |
| zhangsan | 大数据       |
| wangwu   | 人工智能     |
| zhaoliu  | NULL         |
| tianqi   | NULL         |
+----------+--------------+

1.4.2 Right connection

右表主表列全显示,左表匹配到的显示,匹配不到的null

select a.列,b.列 from a表 a(别名) right join b表 b(别名) on a(列)=b(列);

mysql> select a.name,h.hob_name from accp a right join Hob h on a.hobby=h.id;
+----------+--------------+
| name     | hob_name     |
+----------+--------------+
| wangwu   | 大数据       |
| lisi     | 云计算       |
| wangwu   | 人工智能     |
| zhangsan | 大数据       |
| NULL     | 大数据       |
+----------+--------------+

Guess you like

Origin blog.51cto.com/14625831/2547977