https://www.cnblogs.com/clsn/p/8087417.html #auto-id-21
https://www.cnblogs.com/itdragon/p/8194622.html 行锁
注意:NULL值的排序
在MySQL中,把NULL值当做一列值中的最小值对待。
因此,升序排序时,它出现在最前面
ORDER BY的语法结构
order by
SELECT column1, column2, ... FROM table_name ORDER BY column1, column2, ...
ASC|DESC;
案例
mysql> select * from info05;
+----+----------+-------+-------+
| id | name | level | score |
+----+----------+-------+-------+
| 1 | zhangsan | 3 | 89 |
| 2 | lisi | 9 | 56 |
| 3 | wangwu | 5 | 54 |
| 4 | lili | 3 | 78 |
+----+----------+-------+-------+
4 rows in set (0.00 sec)
mysql> select id,name,level from info05 where score>80 order by score desc;
+----+----------+-------+
| id | name | level |
+----+----------+-------+
| 1 | zhangsan | 3 |
+----+----------+-------+
1 row in set (0.00 sec)
如下
mysql> select id,name,level,score from info05 order by level desc,score desc;
+----+----------+-------+-------+
| id | name | level | score |
+----+----------+-------+-------+
| 5 | qq | 9 | 67 |
| 2 | lisi | 9 | 56 |
| 3 | wangwu | 5 | 54 |
| 1 | zhangsan | 3 | 89 |
| 4 | lili | 3 | 78 |
+----+----------+-------+-------+
5 rows in set (0.00 sec)
聚合函数
函数
GROUP BY的语法结构
mysql> SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name;
案例一
mysql> select * from info05;
+----+----------+-------+-------+
| id | name | level | score |
+----+----------+-------+-------+
| 1 | zhangsan | 3 | 89 |
| 2 | lisi | 9 | 56 |
| 3 | wangwu | 5 | 54 |
| 4 | lili | 3 | 78 |
| 5 | qq | 9 | 67 |
+----+----------+-------+-------+
5 rows in set (0.00 sec)
#########################################
对表info05 进行平均值计算然后以等级分组查询。
mysql> select level,avg(score) as avg from info05 group by level;
+-------+---------+
| level | avg |
+-------+---------+
| 3 | 83.5000 |
| 5 | 54.0000 |
| 9 | 61.5000 |
+-------+---------+
3 rows in set (0.00 sec)
案例二:查询最大值
mysql> select name,sum(score) as '最大值' from info05 group by name;
+----------+-----------+
| name | 最大值 |
+----------+-----------+
| lili | 78 |
| lisi | 56 |
| qq | 67 |
| wangwu | 54 |
| zhangsan | 89 |
+----------+-----------+
5 rows in set (0.00 sec)
mysql>
案例三:对player 表中level大于等于45进行计算并分组表示之后按降序显示
mysql> select count(name),level from player where level>=45 group by level order by count(name) desc;
+-------------+-------+
| count(name) | level |
+-------------+-------+
| 4 | 46 |
| 2 | 45 |
| 1 | 47 |
+-------------+-------+
mysql> select level,count(level) as 等级个数 from info05 group by level order by count(level) desc;
+-------+--------------+
| level | 等级个数 |
+-------+--------------+
| 3 | 2 |
| 9 | 2 |
| 5 | 1 |
+-------+--------------+
3 rows in set (0.00 sec)
mysql>
**案例四: 多表查询
mysql> select * from Websites
+----+--------------+---------------------------+-------+---------+
| id | name | url | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1 | Google | https://www.google.cm/ | 1 | USA |
| 2 | 淘宝 | https://www.taobao.com/ | 13 | CN |
| 3 | 菜鸟教程 | http://www.runoob.com/ | 4689 | CN |
| 4 | 微博 | http://weibo.com/ | 20 | CN |
| 5 | Facebook | https://www.facebook.com/ | 3 | USA |
| 7 | stackoverflow | http://stackoverflow.com/ | 0 | IND |
+----+---------------+---------------------------+-------+---------+
9 rows in set (0.00 sec)
mysql> select * from access_log;
+-----+---------+-------+------------+
| aid | site_id | count | date |
+-----+---------+-------+------------+
| 1 | 1 | 45 | 2016-05-10 |
| 2 | 3 | 100 | 2016-05-13 |
| 3 | 1 | 230 | 2016-05-14 |
| 4 | 2 | 10 | 2016-05-14 |
| 5 | 5 | 205 | 2016-05-14 |
| 6 | 4 | 13 | 2016-05-15 |
| 7 | 3 | 220 | 2016-05-15 |
| 8 | 5 | 545 | 2016-05-16 |
| 9 | 3 | 201 | 2016-05-17 |
+-----+---------+-------+------------+
9 rows in set (0.00 sec)
mysql>SELECT Websites.name,COUNT(access_log.aid) AS nums FROM access_log LEFT JOIN Websites ON access_log.site_id=Websites.id
GROUP BY Websites.name;
+------------+-----------+
| name | nums |
+------------+-----------+
| Facebook | 2 |
| Google | 2 |
| 微博 | 1 |
| 淘宝 | 1 |
| 菜鸟教程 | 3 |
+------------+-----------+
5 rows in set (0.00 sec)
LIMIT语法结构
注意
SELECT column1, column2, ... FROM table_name LIMIT [offset,] number;
案例
位置偏移量,从0开始
mysql> select * from info05 limit 1,2;
+----+--------+-------+-------+
| id | name | level | score |
+----+--------+-------+-------+
| 2 | lisi | 9 | 56 |
| 3 | wangwu | 5 | 54 |
+----+--------+-------+-------+
2 rows in set (0.00 sec)
mysql>
mysql> select * from info05 limit 2 offset 3;
+----+------+-------+-------+
| id | name | level | score |
+----+------+-------+-------+
| 4 | lili | 3 | 78 |
| 5 | qq | 9 | 67 |
+----+------+-------+-------+
2 rows in set (0.00 sec)
mysql>
别名的语法结构
SELECT column_name AS alias_name FROM table_name;
SELECT column_name(s) FROM table_name AS table-alias_name;
案例
mysql> select count(*) as number from info05;
+--------+
| number |
+--------+
| 5 |
+--------+
1 row in set (0.01 sec)
注意
as不仅有别名的作用,也有相连的作用
如下作用:
新建表tmp并且将表player的数据复制到表tmp中
mysql> create table tmp as select * from player;
Query OK, 3218 rows affected (0.15 sec)
Records: 3218 Duplicates: 0 Warnings: 0
常用通配符
案例
mysql> select id,name,level from player where name like '%s';
+-----+---------+-------+
| id | name | level |
+-----+---------+-------+
| 448 | useless | 1 |
| 713 | guess | 25 |
+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select id,name,level from player where name like '_uess';
+-----+-------+-------+
| id | name | level |
+-----+-------+-------+
| 713 | guess | 25 |
+-----+-------+-------+
1 row in set (0.01 sec)
mysql> select id,name,level from player where name like '_es%';
+------+---------+-------+
| id | name | level |
+------+---------+-------+
| 2237 | leslieF | 3 |
+------+---------+-------+
1 row in set (0.01 sec)
mysql> select name,level from player where id in (select id from player where level>=45);
mysql> insert into tmp select * from player where id in (select id from player);
Query OK, 3218 rows affected (0.12 sec)
Records: 3218 Duplicates: 0 Warnings: 0
mysql> update tmp set level = level - 7 where id in (select a.id from (select id from tmp where level >= 47) a);
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> delete from tmp where id in (select a.id from (select id from tmp where level=47) a);
Query OK, 1 row affected (0.01 sec)
mysql> select id,name,level from tmp where id = (select id from tmp where name='shirley');
mysql> select count(*) as number from tmp where EXISTS (相当于if)(select id from tmp where name='shirley');
注意
exists 用于判断后面的语句是否正确,如果正确那么执行外语句。如果不正确就不执行。
exists 后面的语句只能用select 如果select是正确的那么执行上一个语句,如果select筛选错误,那就不执行上一个语句
NULL值和空值的区别
mysql> select * from info05 where name is not null;
+----+----------+-------+-------+
| id | name | level | score |
+----+----------+-------+-------+
| 1 | zhangsan | 3 | 89 |
| 2 | lisi | 9 | 56 |
| 3 | wangwu | 5 | 54 |
| 4 | lili | 3 | 78 |
| 5 | qq | 9 | 67 |
| 6 | | 9 | 98 |
| 7 | pp | NULL | 88 |
+----+----------+-------+-------+
7 rows in set (0.00 sec)
mysql> select * from info05 where level is null;
+----+------+-------+-------+
| id | name | level | score |
+----+------+-------+-------+
| 7 | pp | NULL | 88 |
+----+------+-------+-------+
1 row in set (0.01 sec)
mysql>
^ 匹配开始字符
$ 匹配结束字符
. 匹配任意单个字符
* 匹配任意个前面的字符
+ 匹配前面字符至少1次
p1|p2 匹配p1或p2
[...] 匹配字符集中的任意一个字符
[^...] 匹配不在中括号内的任何字符
{n} 匹配前面的字符串n次
{n,m} 匹配前面的字符串至少n次,至多m次
案例
mysql> select * from info05 where name regexp '[^wangwu]';
+----+----------+-------+-------+
| id | name | level | score |
+----+----------+-------+-------+
| 1 | zhangsan | 3 | 89 |
| 2 | lisi | 9 | 56 |
| 4 | lili | 3 | 78 |
| 5 | qq | 9 | 67 |
+----+----------+-------+-------+
4 rows in set (0.00 sec)
mysql> select * from info05 where name regexp '[^w]';
+----+----------+-------+-------+
| id | name | level | score |
+----+----------+-------+-------+
| 1 | zhangsan | 3 | 89 |
| 2 | lisi | 9 | 56 |
| 3 | wangwu | 5 | 54 |
| 4 | lili | 3 | 78 |
| 5 | qq | 9 | 67 |
+----+----------+-------+-------+
5 rows in set (0.00 sec)
mysql> select * from info05 where name regexp 'w+';
+----+--------+-------+-------+
| id | name | level | score |
+----+--------+-------+-------+
| 3 | wangwu | 5 | 54 |
+----+--------+-------+-------+
1 row in set (0.00 sec)
mysql> select * from info05 where name regexp 'a{1,2}';
+----+----------+-------+-------+
| id | name | level | score |
+----+----------+-------+-------+
| 1 | zhangsan | 3 | 89 |
| 3 | wangwu | 5 | 54 |
+----+----------+-------+-------+
2 rows in set (0.00 sec)
mysql>
mysql> select * from info05 where name regexp 'l.';
+----+------+-------+-------+
| id | name | level | score |
+----+------+-------+-------+
| 2 | lisi | 9 | 56 |
| 4 | lili | 3 | 78 |
+----+------+-------+-------+
2 rows in set (0.00 sec)
mysql>
mysql> select * from info05 where name regexp 'a|c';
+----+----------+-------+-------+
| id | name | level | score |
+----+----------+-------+-------+
| 1 | zhangsan | 3 | 89 |
| 3 | wangwu | 5 | 54 |
+----+----------+-------+-------+
2 rows in set (0.00 sec)
用于对记录中的字段值进行运算
+ 加法
- 减法
* 乘法
/ 除法
% 取余数
案例
mysql> select 1+2 as addition, 2-1 as subtraction, 2*3 as multiplication, 4/2 as division, 7%2 as remainder;
+----------+-------------+----------------+----------+-----------+
| addition | subtraction | multiplication | division | remainder |
+----------+-------------+----------------+----------+-----------+
| 3 | 1 | 6 | 2.0000 | 1 |
+----------+-------------+----------------+----------+-----------+
1 row in set (0.01 sec)
mysql> select greatest(1,2,3);
+-----------------+
| greatest(1,2,3) |
+-----------------+
| 3 |
+-----------------+
1 row in set (0.00 sec)
mysql> select greatest(1,2,3) as '最大值';
+-----------+
| 最大值 |
+-----------+
| 3 |
+-----------+
1 row in set (0.00 sec)
mysql> select 2 in(1,2,3), 'c' in('b','d');
+-------------+-----------------+
| 2 in(1,2,3) | 'c' in('b','d') |
+-------------+-----------------+
| 1 | 0 |
+-------------+-----------------+
1 row in set (0.00 sec)
mysql> select 'bdqn' like 'bdq_';
+--------------------+
| 'bdqn' like 'bdq_' |
+--------------------+
| 1 |
+--------------------+
1 row in set (0.00 sec)
mysql> select 'bdqn' like 'bdq_','kgc' like '%c', 'etc' not like '%th';
+--------------------+-----------------+----------------------+
| 'bdqn' like 'bdq_' | 'kgc' like '%c' | 'etc' not like '%th' |
+--------------------+-----------------+----------------------+
| 1 | 1 | 1 |
+--------------------+-----------------+----------------------+
1 row in set (0.00 sec)
案例
mysql> SELECT 2 OR 3,4 || 0,0 OR NULL,1 || NULL;
+--------+--------+-----------+-----------+
| 2 OR 3 | 4 || 0 | 0 OR NULL | 1 || NULL |
+--------+--------+-----------+-----------+
| 1 | 1 | NULL | 1 |
+--------+--------+-----------+-----------+
1 row in set (0.00 sec)
mysql> SELECT 2 XOR 3,0 XOR 0,0 XOR 5,1 XOR NULL,NULL XOR NULL;
+---------+---------+---------+------------+---------------+
| 2 XOR 3 | 0 XOR 0 | 0 XOR 5 | 1 XOR NULL | NULL XOR NULL |
+---------+---------+---------+------------+---------------+
| 0 | 0 | 1 | NULL | NULL |
+---------+---------+---------+------------+---------------+
1 row in set (0.00 sec)
mysql> select 2 and 3, 0 and 1, 0 and null, 1 and null;
+---------+---------+------------+------------+
| 2 and 3 | 0 and 1 | 0 and null | 1 and null |
+---------+---------+------------+------------+
| 1 | 0 | 0 | NULL |
+---------+---------+------------+------------+
1 row in set (0.00 sec)
mysql>
对二进制数进行计算的运算符
常用的位运算符
& 按位与
| 按位或
~ 按位取反
^ 按位异或
<< 按位左移
>> 按位右移
案例
mysql> SELECT 10 & 15, 10 | 15, 10 ^ 15, 5 &~1;
+---------+---------+---------+-------+
| 10 & 15 | 10 | 15 | 10 ^ 15 | 5 &~1 |
+---------+---------+---------+-------+
| 10 | 15 | 5 | 4 |
+---------+---------+---------+-------+
1 row in set (0.00 sec)
mysql> SELECT 1<<2, 2<<2,10>>2,15>>2;
+------+------+-------+-------+
| 1<<2 | 2<<2 | 10>>2 | 15>>2 |
+------+------+-------+-------+
| 4 | 8 | 2 | 3 |
+------+------+-------+-------+
1 row in set (0.00 sec)
语法结构
select column_name(s) from table1 inner join table2 ON table1.column_name = table2.column_name;
实现原理
案例
mysql> select a_id,a_name,a_level from a_player a inner join b_player b on a_id=b_id;
+------+--------+---------+
| a_id | a_name | a_level |
+------+--------+---------+
| 2 | bbbb | 20 |
| 3 | cccc | 30 |
+------+--------+---------+
2 rows in set (0.00 sec)
mysql> select * from a_player a left join b_player b on a.a_id=b.b_id;
+------+--------+---------+------+--------+---------+
| a_id | a_name | a_level | b_id | b_name | b_level |
+------+--------+---------+------+--------+---------+
| 2 | bbbb | 20 | 2 | bbbb | 20 |
| 3 | cccc | 30 | 3 | cccc | 30 |
| 1 | aaaa | 10 | NULL | NULL | NULL |
| 4 | dddd | 40 | NULL | NULL | NULL |
+------+--------+---------+------+--------+---------+
4 rows in set (0.00 sec)
mysql> select * from a_player a right join b_player b on a.a_id=b.b_id;
+------+--------+---------+------+--------+---------+
| a_id | a_name | a_level | b_id | b_name | b_level |
+------+--------+---------+------+--------+---------+
| 2 | bbbb | 20 | 2 | bbbb | 20 |
| 3 | cccc | 30 | 3 | cccc | 30 |
| NULL | NULL | NULL | 5 | eeee | 50 |
| NULL | NULL | NULL | 6 | ffff | 60 |
+------+--------+---------+------+--------+---------+
4 rows in set (0.00 sec)