必备SQL

11 篇文章 0 订阅

必备SQL和表关系以及授权

数据准备

mysql> create table depart(
    -> id int auto_increment primary key not null,
    -> title varchar(16) not null
    -> )default charset=utf8;
Query OK, 0 rows affected (0.04 sec)
create table info (
    -> id int not null auto_increment primary key,
    -> name varchar(16) not null,
    -> email varchar(32) not null,
    -> age int(11) not null,
    -> depart_id int(11) not null
    -> ) default charset=utf8;
Query OK, 0 rows affected (0.03 sec)
mysql> insert into depart(title) values ("开发"),("运营"),("销售");
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0
import pymysql

conn = pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    passwd='20020115',
    db='user',
    charset='utf8'
)

cursor = conn.cursor()
cursor.execute('insert into info (name,email,age,depart_id) values '
               '("jomi","18005309753@163com",33,1),'
               '("丁佳洪","15732539018@163.com",20,2),'
               '("丁彩霞","18781265179@163.com",30,3),'
               '("丁思涵","17600922109@163.com",24,3),'
               '("丁洁","13502042265@163.com",22,2),'
               '("丁贤源","18910318421@163.com",43,1),'
               '("么雪松","18500387457@163.com",39,2),'
               '("于姗姗","18810371850@163.com",31,2),'
               '("于玺","18810371850@163.com",43,2),'
               '("付强","18010423303@163.com",42,1),'
               '("付晓杰","19931514305@163.com",22,3)')
conn.commit()

条件查询

根据条件搜索结果

select * from 表名 where 列名 范围的符号(<,>,=,!=,>=,<=) 30;
select * from 表名 where 列名 between 值1 and 值2;  -- 获取在值1和值2之间,包括值1和值2数据
select * from 表名 where 列名 in (值1,值2,值3,····); -- 获取 列名为值1,值2,值3的数据
select * from 表名 where 列名 not in (值1,值2,值3,····); -- 获取 列名不为值1,值2,值3的数据
select * from 表名 where 列名 in (子查询); -- 将查询的结果作为条件进行搜索
select * from 表名 where exists (子查询); -- 判断子查询是否存在如果不存在不进行搜索,存在搜索
select * from 表名 where not exists (子查询); -- 顾名思义,和上面的相反
select * from (select * from 表名 where 列名 > 值2) as 临时的表的别名 WHERE 列名 > 值1;
mysql> select * from info where age > 30;
+----+--------+---------------------+-----+-----------+
| id | name   | email               | age | depart_id |
+----+--------+---------------------+-----+-----------+
|  2 | jomi   | 18005309753@163com  |  33 |         1 |
|  7 | 丁贤源 | 18910318421@163.com |  43 |         1 |
|  8 | 么雪松 | 18500387457@163.com |  39 |         2 |
|  9 | 于姗姗 | 18810371850@163.com |  31 |         2 |
| 10 | 于玺   | 18810371850@163.com |  43 |         2 |
| 11 | 付强   | 18010423303@163.com |  42 |         1 |
+----+--------+---------------------+-----+-----------+
6 rows in set (0.01 sec)
-- 查询id大于1的数据
mysql> select * from info where id > 1;
+----+--------+---------------------+-----+-----------+
| id | name   | email               | age | depart_id |
+----+--------+---------------------+-----+-----------+
|  2 | jomi   | 18005309753@163com  |  33 |         1 |
|  3 | 丁佳洪 | 15732539018@163.com |  20 |         2 |
|  4 | 丁彩霞 | 18781265179@163.com |  30 |         3 |
|  5 | 丁思涵 | 17600922109@163.com |  24 |         3 |
|  6 | 丁洁   | 13502042265@163.com |  22 |         2 |
|  7 | 丁贤源 | 18910318421@163.com |  43 |         1 |
|  8 | 么雪松 | 18500387457@163.com |  39 |         2 |
|  9 | 于姗姗 | 18810371850@163.com |  31 |         2 |
| 10 | 于玺   | 18810371850@163.com |  43 |         2 |
| 11 | 付强   | 18010423303@163.com |  42 |         1 |
| 12 | 付晓杰 | 19931514305@163.com |  22 |         3 |
+----+--------+---------------------+-----+-----------+
11 rows in set (0.00 sec)

mysql> select * from info where id = 1;
+----+---------+----------------+-----+-----------+
| id | name    | email          | age | depart_id |
+----+---------+----------------+-----+-----------+
|  1 | JackHao | 188127@163.com |  24 |         1 |
+----+---------+----------------+-----+-----------+
1 row in set (0.00 sec)

mysql> select * from info where id >= 1;
+----+---------+---------------------+-----+-----------+
| id | name    | email               | age | depart_id |
+----+---------+---------------------+-----+-----------+
|  1 | JackHao | 188127@163.com      |  24 |         1 |
|  2 | jomi    | 18005309753@163com  |  33 |         1 |
|  3 | 丁佳洪  | 15732539018@163.com |  20 |         2 |
|  4 | 丁彩霞  | 18781265179@163.com |  30 |         3 |
|  5 | 丁思涵  | 17600922109@163.com |  24 |         3 |
|  6 | 丁洁    | 13502042265@163.com |  22 |         2 |
|  7 | 丁贤源  | 18910318421@163.com |  43 |         1 |
|  8 | 么雪松  | 18500387457@163.com |  39 |         2 |
|  9 | 于姗姗  | 18810371850@163.com |  31 |         2 |
| 10 | 于玺    | 18810371850@163.com |  43 |         2 |
| 11 | 付强    | 18010423303@163.com |  42 |         1 |
| 12 | 付晓杰  | 19931514305@163.com |  22 |         3 |
+----+---------+---------------------+-----+-----------+
12 rows in set (0.00 sec)

mysql> select * from info where id != 1;
+----+--------+---------------------+-----+-----------+
| id | name   | email               | age | depart_id |
+----+--------+---------------------+-----+-----------+
|  2 | jomi   | 18005309753@163com  |  33 |         1 |
|  3 | 丁佳洪 | 15732539018@163.com |  20 |         2 |
|  4 | 丁彩霞 | 18781265179@163.com |  30 |         3 |
|  5 | 丁思涵 | 17600922109@163.com |  24 |         3 |
|  6 | 丁洁   | 13502042265@163.com |  22 |         2 |
|  7 | 丁贤源 | 18910318421@163.com |  43 |         1 |
|  8 | 么雪松 | 18500387457@163.com |  39 |         2 |
|  9 | 于姗姗 | 18810371850@163.com |  31 |         2 |
| 10 | 于玺   | 18810371850@163.com |  43 |         2 |
| 11 | 付强   | 18010423303@163.com |  42 |         1 |
| 12 | 付晓杰 | 19931514305@163.com |  22 |         3 |
+----+--------+---------------------+-----+-----------+
11 rows in set (0.00 sec)

mysql> select * from info where id between 2 and 4;
+----+--------+---------------------+-----+-----------+
| id | name   | email               | age | depart_id |
+----+--------+---------------------+-----+-----------+
|  2 | jomi   | 18005309753@163com  |  33 |         1 |
|  3 | 丁佳洪 | 15732539018@163.com |  20 |         2 |
|  4 | 丁彩霞 | 18781265179@163.com |  30 |         3 |
+----+--------+---------------------+-----+-----------+
3 rows in set (0.00 sec)

mysql> select * from info where id in (1,4,6);
+----+---------+---------------------+-----+-----------+
| id | name    | email               | age | depart_id |
+----+---------+---------------------+-----+-----------+
|  1 | JackHao | 188127@163.com      |  24 |         1 |
|  4 | 丁彩霞  | 18781265179@163.com |  30 |         3 |
|  6 | 丁洁    | 13502042265@163.com |  22 |         2 |
+----+---------+---------------------+-----+-----------+
3 rows in set (0.00 sec)

mysql> select * from info where id not in (1,3,5);
+----+--------+---------------------+-----+-----------+
| id | name   | email               | age | depart_id |
+----+--------+---------------------+-----+-----------+
|  2 | jomi   | 18005309753@163com  |  33 |         1 |
|  4 | 丁彩霞 | 18781265179@163.com |  30 |         3 |
|  6 | 丁洁   | 13502042265@163.com |  22 |         2 |
|  7 | 丁贤源 | 18910318421@163.com |  43 |         1 |
|  8 | 么雪松 | 18500387457@163.com |  39 |         2 |
|  9 | 于姗姗 | 18810371850@163.com |  31 |         2 |
| 10 | 于玺   | 18810371850@163.com |  43 |         2 |
| 11 | 付强   | 18010423303@163.com |  42 |         1 |
| 12 | 付晓杰 | 19931514305@163.com |  22 |         3 |
+----+--------+---------------------+-----+-----------+
9 rows in set (0.00 sec)

-- 这种情况下子查询的结果只能出现一列数据(否则就会报错)
mysql> select * from info where id in (select id from depart);
+----+---------+---------------------+-----+-----------+
| id | name    | email               | age | depart_id |
+----+---------+---------------------+-----+-----------+
|  1 | JackHao | 188127@163.com      |  24 |         1 |
|  2 | jomi    | 18005309753@163com  |  33 |         1 |
|  3 | 丁佳洪  | 15732539018@163.com |  20 |         2 |
+----+---------+---------------------+-----+-----------+
3 rows in set (0.04 sec)
-- 这里就是报错
mysql> select * from info where id in (select id,title from depart);
ERROR 1241 (21000): Operand should contain 1 column(s)

mysql> select * from info where exists (select * from depart where id = 5); -- 子查询不存在
Empty set (0.00 sec)

mysql> select * from info where exists (select * from depart where id = 1); -- 子查询存在
+----+---------+---------------------+-----+-----------+
| id | name    | email               | age | depart_id |
+----+---------+---------------------+-----+-----------+
|  1 | JackHao | 188127@163.com      |  24 |         1 |
|  2 | jomi    | 18005309753@163com  |  33 |         1 |
|  3 | 丁佳洪  | 15732539018@163.com |  20 |         2 |
|  4 | 丁彩霞  | 18781265179@163.com |  30 |         3 |
|  5 | 丁思涵  | 17600922109@163.com |  24 |         3 |
|  6 | 丁洁    | 13502042265@163.com |  22 |         2 |
|  7 | 丁贤源  | 18910318421@163.com |  43 |         1 |
|  8 | 么雪松  | 18500387457@163.com |  39 |         2 |
|  9 | 于姗姗  | 18810371850@163.com |  31 |         2 |
| 10 | 于玺    | 18810371850@163.com |  43 |         2 |
| 11 | 付强    | 18010423303@163.com |  42 |         1 |
| 12 | 付晓杰  | 19931514305@163.com |  22 |         3 |
+----+---------+---------------------+-----+-----------+
12 rows in set (0.00 sec)

mysql> select * from (select * from info where id > 4) as T WHERE age > 30;
+----+--------+---------------------+-----+-----------+
| id | name   | email               | age | depart_id |
+----+--------+---------------------+-----+-----------+
|  7 | 丁贤源 | 18910318421@163.com |  43 |         1 |
|  8 | 么雪松 | 18500387457@163.com |  39 |         2 |
|  9 | 于姗姗 | 18810371850@163.com |  31 |         2 |
| 10 | 于玺   | 18810371850@163.com |  43 |         2 |
| 11 | 付强   | 18010423303@163.com |  42 |         1 |
+----+--------+---------------------+-----+-----------+
5 rows in set (0.00 sec)

通配符

一般用于模糊查询使用

select * from info where name like "于%"; -- `%` 可以匹配多个字符
select * from info where name like "于_"; -- `_` 只能匹配一个字符
select * from info where name like "_a%"; -- 两种可以结合使用
mysql> select * from info where name like "于%";
+----+--------+---------------------+-----+-----------+
| id | name   | email               | age | depart_id |
+----+--------+---------------------+-----+-----------+
|  9 | 于姗姗 | 18810371850@163.com |  31 |         2 |
| 10 | 于玺   | 18810371850@163.com |  43 |         2 |
+----+--------+---------------------+-----+-----------+
2 rows in set (0.02 sec)

mysql> select * from info where name like "于_";
+----+------+---------------------+-----+-----------+
| id | name | email               | age | depart_id |
+----+------+---------------------+-----+-----------+
| 10 | 于玺 | 18810371850@163.com |  43 |         2 |
+----+------+---------------------+-----+-----------+
1 row in set (0.00 sec)

mysql> select * from info where name like "_a%";
+----+---------+----------------+-----+-----------+
| id | name    | email          | age | depart_id |
+----+---------+----------------+-----+-----------+
|  1 | JackHao | 188127@163.com |  24 |         1 |
+----+---------+----------------+-----+-----------+
1 row in set (0.00 sec)

在数据量比较小时候可以用这种方式进行搜索,如果数据量比较大的时候不建议使用这种方法,因为这种方法的搜索效率很低

指定列(映射)

select 列名,列名 as 列别名 from 列名;
select 列名,列名 as 列别名,123 from 列名;
select 列名,列名,列名 as 列别名,(select max(列名) from 表1) as 列别名,列名 from 表2;
 select id,name,case when depart_id = 1 then "第一部门" end v1 from info;  -- 判断
mysql> select id,name from info;
+----+---------+
| id | name    |
+----+---------+
|  1 | JackHao |
|  2 | jomi    |
|  3 | 丁佳洪  |
|  4 | 丁彩霞  |
|  5 | 丁思涵  |
|  6 | 丁洁    |
|  7 | 丁贤源  |
|  8 | 么雪松  |
|  9 | 于姗姗  |
| 10 | 于玺    |
| 11 | 付强    |
| 12 | 付晓杰  |
+----+---------+
12 rows in set (0.00 sec)

mysql> select id,name as nm from info;
+----+---------+
| id | nm      |
+----+---------+
|  1 | JackHao |
|  2 | jomi    |
|  3 | 丁佳洪  |
|  4 | 丁彩霞  |
|  5 | 丁思涵  |
|  6 | 丁洁    |
|  7 | 丁贤源  |
|  8 | 么雪松  |
|  9 | 于姗姗  |
| 10 | 于玺    |
| 11 | 付强    |
| 12 | 付晓杰  |
+----+---------+
12 rows in set (0.00 sec)

mysql> select id,name as nm,123 from info;
+----+---------+-----+
| id | nm      | 123 |
+----+---------+-----+
|  1 | JackHao | 123 |
|  2 | jomi    | 123 |
|  3 | 丁佳洪  | 123 |
|  4 | 丁彩霞  | 123 |
|  5 | 丁思涵  | 123 |
|  6 | 丁洁    | 123 |
|  7 | 丁贤源  | 123 |
|  8 | 么雪松  | 123 |
|  9 | 于姗姗  | 123 |
| 10 | 于玺    | 123 |
| 11 | 付强    | 123 |
| 12 | 付晓杰  | 123 |
+----+---------+-----+
12 rows in set (0.00 sec)

注意:在实际的生产环境中尽量减少使用SEELCT *

mysql> select id,name,666 as num,
    -> (select max(id) from depart) as mid,
    -> (select min(id) from depart) as nid,
    -> age from info;
+----+---------+-----+------+------+-----+
| id | name    | num | mid  | nid  | age |
+----+---------+-----+------+------+-----+
|  1 | JackHao | 666 |    3 |    1 |  24 |
|  2 | jomi    | 666 |    3 |    1 |  33 |
|  3 | 丁佳洪  | 666 |    3 |    1 |  20 |
|  4 | 丁彩霞  | 666 |    3 |    1 |  30 |
|  5 | 丁思涵  | 666 |    3 |    1 |  24 |
|  6 | 丁洁    | 666 |    3 |    1 |  22 |
|  7 | 丁贤源  | 666 |    3 |    1 |  43 |
|  8 | 么雪松  | 666 |    3 |    1 |  39 |
|  9 | 于姗姗  | 666 |    3 |    1 |  31 |
| 10 | 于玺    | 666 |    3 |    1 |  43 |
| 11 | 付强    | 666 |    3 |    1 |  42 |
| 12 | 付晓杰  | 666 |    3 |    1 |  22 |
+----+---------+-----+------+------+-----+
12 rows in set (0.01 sec)

-- 查询的数据比较低
mysql> select id,name,(select title from depart where depart.id=info.depart_id) as x1 from info;
+----+---------+------+
| id | name    | x1   |
+----+---------+------+
|  1 | JackHao | 开发 |
|  2 | jomi    | 开发 |
|  3 | 丁佳洪  | 运营 |
|  4 | 丁彩霞  | 销售 |
|  5 | 丁思涵  | 销售 |
|  6 | 丁洁    | 运营 |
|  7 | 丁贤源  | 开发 |
|  8 | 么雪松  | 运营 |
|  9 | 于姗姗  | 运营 |
| 10 | 于玺    | 运营 |
| 11 | 付强    | 开发 |
| 12 | 付晓杰  | 销售 |
+----+---------+------+
12 rows in set (0.00 sec)

mysql> select id,name,
    -> case when depart_id = 1 then "第一部门" end v1 from info;
+----+---------+----------+
| id | name    | v1       |
+----+---------+----------+
|  1 | JackHao | 第一部门 |
|  2 | jomi    | 第一部门 |
|  3 | 丁佳洪  | NULL     |
|  4 | 丁彩霞  | NULL     |
|  5 | 丁思涵  | NULL     |
|  6 | 丁洁    | NULL     |
|  7 | 丁贤源  | 第一部门 |
|  8 | 么雪松  | NULL     |
|  9 | 于姗姗  | NULL     |
| 10 | 于玺    | NULL     |
| 11 | 付强    | 第一部门 |
| 12 | 付晓杰  | NULL     |
+----+---------+----------+
12 rows in set (0.01 sec)

mysql> select id,name,case depart_id when 1 then "第一部门" else "其他" end v1 from info;
+----+---------+----------+
| id | name    | v1       |
+----+---------+----------+
|  1 | JackHao | 第一部门 |
|  2 | jomi    | 第一部门 |
|  3 | 丁佳洪  | 其他     |
|  4 | 丁彩霞  | 其他     |
|  5 | 丁思涵  | 其他     |
|  6 | 丁洁    | 其他     |
|  7 | 丁贤源  | 第一部门 |
|  8 | 么雪松  | 其他     |
|  9 | 于姗姗  | 其他     |
| 10 | 于玺    | 其他     |
| 11 | 付强    | 第一部门 |
| 12 | 付晓杰  | 其他     |
+----+---------+----------+
12 rows in set (0.00 sec)

mysql> select id,name,case depart_id when 1 then "第一部门" when 2 then "第二部门" else "其他" end v1 from info;
+----+---------+----------+
| id | name    | v1       |
+----+---------+----------+
|  1 | JackHao | 第一部门 |
|  2 | jomi    | 第一部门 |
|  3 | 丁佳洪  | 第二部门 |
|  4 | 丁彩霞  | 其他     |
|  5 | 丁思涵  | 其他     |
|  6 | 丁洁    | 第二部门 |
|  7 | 丁贤源  | 第一部门 |
|  8 | 么雪松  | 第二部门 |
|  9 | 于姗姗  | 第二部门 |
| 10 | 于玺    | 第二部门 |
| 11 | 付强    | 第一部门 |
| 12 | 付晓杰  | 其他     |
+----+---------+----------+
12 rows in set (0.00 sec)

 select id,name,case when age < 18 then "少年" when age<30 then "青年" else "其他" end v1 from info;
+----+---------+------+
| id | name    | v1   |
+----+---------+------+
|  1 | JackHao | 青年 |
|  2 | jomi    | 其他 |
|  3 | 丁佳洪  | 青年 |
|  4 | 丁彩霞  | 其他 |
|  5 | 丁思涵  | 青年 |
|  6 | 丁洁    | 青年 |
|  7 | 丁贤源  | 其他 |
|  8 | 么雪松  | 其他 |
|  9 | 于姗姗  | 其他 |
| 10 | 于玺    | 其他 |
| 11 | 付强    | 其他 |
| 12 | 付晓杰  | 青年 |
+----+---------+------+
12 rows in set (0.00 sec)

排序

对数据进行排序

select * from 表名 order by 列名 desc; -- 根据id 进行倒叙排序
select * from 表名 order by 列名 asc; -- 根据id 进行正叙排序
select 列名,列名,列名 from 表名 order by 列名1 asc, 列名2 desc; -- 首先根据列名1进行排序如果列名1相同了,再根据列名2进行排序
select id,name,age from info where id>6 order by id desc; -- where 的具有更高的优先级
select * from 表名 limit 5 offset 2;  -- 从第二个向后取5条数据
mysql> select * from info order by id desc;
+----+---------+---------------------+-----+-----------+
| id | name    | email               | age | depart_id |
+----+---------+---------------------+-----+-----------+
| 12 | 付晓杰  | 19931514305@163.com |  22 |         3 |
| 11 | 付强    | 18010423303@163.com |  42 |         1 |
| 10 | 于玺    | 18810371850@163.com |  43 |         2 |
|  9 | 于姗姗  | 18810371850@163.com |  31 |         2 |
|  8 | 么雪松  | 18500387457@163.com |  39 |         2 |
|  7 | 丁贤源  | 18910318421@163.com |  43 |         1 |
|  6 | 丁洁    | 13502042265@163.com |  22 |         2 |
|  5 | 丁思涵  | 17600922109@163.com |  24 |         3 |
|  4 | 丁彩霞  | 18781265179@163.com |  30 |         3 |
|  3 | 丁佳洪  | 15732539018@163.com |  20 |         2 |
|  2 | jomi    | 18005309753@163com  |  33 |         1 |
|  1 | JackHao | 188127@163.com      |  24 |         1 |
+----+---------+---------------------+-----+-----------+
12 rows in set (0.00 sec)

mysql>  select id,name,age from info order by age asc, id desc;
+----+---------+-----+
| id | name    | age |
+----+---------+-----+
|  3 | 丁佳洪  |  20 |
| 12 | 付晓杰  |  22 |
|  6 | 丁洁    |  22 |
|  5 | 丁思涵  |  24 |
|  1 | JackHao |  24 |
|  4 | 丁彩霞  |  30 |
|  9 | 于姗姗  |  31 |
|  2 | jomi    |  33 |
|  8 | 么雪松  |  39 |
| 11 | 付强    |  42 |
| 10 | 于玺    |  43 |
|  7 | 丁贤源  |  43 |
+----+---------+-----+
12 rows in set (0.00 sec)

mysql> select id,name,age from info where id>6 order by id desc;
+----+--------+-----+
| id | name   | age |
+----+--------+-----+
| 12 | 付晓杰 |  22 |
| 11 | 付强   |  42 |
| 10 | 于玺   |  43 |
|  9 | 于姗姗 |  31 |
|  8 | 么雪松 |  39 |
|  7 | 丁贤源 |  43 |
+----+--------+-----+
6 rows in set (0.00 sec)

查询部分数据(limit)

一般用于获取部分数据

select * from 表名 limit 5;  -- 显示默认排序的前5个
select * from 表名 order by 列名 desc limit 5; -- 显示倒序的前五个
mysql> select * from info limit 5;
+----+---------+---------------------+-----+-----------+
| id | name    | email               | age | depart_id |
+----+---------+---------------------+-----+-----------+
|  1 | JackHao | 188127@163.com      |  24 |         1 |
|  2 | jomi    | 18005309753@163com  |  33 |         1 |
|  3 | 丁佳洪  | 15732539018@163.com |  20 |         2 |
|  4 | 丁彩霞  | 18781265179@163.com |  30 |         3 |
|  5 | 丁思涵  | 17600922109@163.com |  24 |         3 |
+----+---------+---------------------+-----+-----------+
5 rows in set (0.00 sec)

mysql> select * from info order by id desc limit 5;
+----+--------+---------------------+-----+-----------+
| id | name   | email               | age | depart_id |
+----+--------+---------------------+-----+-----------+
| 12 | 付晓杰 | 19931514305@163.com |  22 |         3 |
| 11 | 付强   | 18010423303@163.com |  42 |         1 |
| 10 | 于玺   | 18810371850@163.com |  43 |         2 |
|  9 | 于姗姗 | 18810371850@163.com |  31 |         2 |
|  8 | 么雪松 | 18500387457@163.com |  39 |         2 |
+----+--------+---------------------+-----+-----------+
5 rows in set (0.00 sec)

mysql> select * from info limit 5 offset 2;
+----+--------+---------------------+-----+-----------+
| id | name   | email               | age | depart_id |
+----+--------+---------------------+-----+-----------+
|  3 | 丁佳洪 | 15732539018@163.com |  20 |         2 |
|  4 | 丁彩霞 | 18781265179@163.com |  30 |         3 |
|  5 | 丁思涵 | 17600922109@163.com |  24 |         3 |
|  6 | 丁洁   | 13502042265@163.com |  22 |         2 |
|  7 | 丁贤源 | 18910318421@163.com |  43 |         1 |
+----+--------+---------------------+-----+-----------+
5 rows in set (0.00 sec)

分组

select 列名,聚合函数(列名),聚合函数(列名),聚合函数(列名),聚合函数(列名) from 表名 group by 分组列名;
select列名,聚合函数(列名) from 表名 group by 列名 having count(列名2) > 2;
mysql> select age,max(id),min(id),count(id),avg(id) from info group by age;
+-----+---------+---------+-----------+---------+
| age | max(id) | min(id) | count(id) | avg(id) |
+-----+---------+---------+-----------+---------+
|  20 |       3 |       3 |         1 |  3.0000 |
|  22 |      12 |       6 |         2 |  9.0000 |
|  24 |       5 |       1 |         2 |  3.0000 |
|  30 |       4 |       4 |         1 |  4.0000 |
|  31 |       9 |       9 |         1 |  9.0000 |
|  33 |       2 |       2 |         1 |  2.0000 |
|  39 |       8 |       8 |         1 |  8.0000 |
|  42 |      11 |      11 |         1 | 11.0000 |
|  43 |      10 |       7 |         2 |  8.5000 |
+-----+---------+---------+-----------+---------+
9 rows in set (0.00 sec)

mysql> select depart_id,count(id) from info group by depart_id;
+-----------+-----------+
| depart_id | count(id) |
+-----------+-----------+
|         1 |         4 |
|         2 |         5 |
|         3 |         3 |
+-----------+-----------+
3 rows in set (0.00 sec)

mysql> select depart_id,count(id) from info group by depart_id having count(id) > 2;
+-----------+-----------+
| depart_id | count(id) |
+-----------+-----------+
|         1 |         4 |
|         2 |         5 |
|         3 |         3 |
+-----------+-----------+
3 rows in set (0.00 sec)
到目前为止sql执行顺序:
	where
	group by
	having
	order by 
	limit
mysql> select age,count(id) from info where id > 2 group by age having count(id) > 1 order by age desc limit 1;
+-----+-----------+
| age | count(id) |
+-----+-----------+
|  43 |         2 |
+-----+-----------+
1 row in set (0.00 sec)

-- 要查询的表info
-- 条件 id>2
-- 根据age分组
-- 对分组后的数据再根据聚合条件过滤 count(id) > 1
-- 根据age从大到小排序
-- 获取第一条

左右连表

多个表连起来查询

外链接

select * from info outer join depart on info.depart_id=info.id;
select info.id,info.name,depart.title from info left  outer join depart on info.depart_id=depart.id; -- info为主表 depart为辅助表
select info.id,info.name,depart.title from info left  outer join depart on info.depart_id=depart.id; -- depart 为主表,info为辅表
mysql> select info.id,info.name,info.email,depart.title from info right outer join depart on info.depart_id=depart.id;
+------+---------+---------------------+-------+
| id   | name    | email               | title |
+------+---------+---------------------+-------+
|    1 | JackHao | 188127@163.com      | 开发  |
|    2 | jomi    | 18005309753@163com  | 开发  |
|    3 | 丁佳洪  | 15732539018@163.com | 运营  |
|    4 | 丁彩霞  | 18781265179@163.com | 销售  |
|    5 | 丁思涵  | 17600922109@163.com | 销售  |
|    6 | 丁洁    | 13502042265@163.com | 运营  |
|    7 | 丁贤源  | 18910318421@163.com | 开发  |
|    8 | 么雪松  | 18500387457@163.com | 运营  |
|    9 | 于姗姗  | 18810371850@163.com | 运营  |
|   10 | 于玺    | 18810371850@163.com | 运营  |
|   11 | 付强    | 18010423303@163.com | 开发  |
|   12 | 付晓杰  | 19931514305@163.com | 销售  |
+------+---------+---------------------+-------+
12 rows in set (0.02 sec)


mysql> select info.id,info.name,depart.title from info left  outer join depart on info.depart_id=depart.id;
+----+---------+-------+
| id | name    | title |
+----+---------+-------+
|  1 | JackHao | 开发  |
|  2 | jomi    | 开发  |
|  7 | 丁贤源  | 开发  |
| 11 | 付强    | 开发  |
|  3 | 丁佳洪  | 运营  |
|  6 | 丁洁    | 运营  |
|  8 | 么雪松  | 运营  |
|  9 | 于姗姗  | 运营  |
| 10 | 于玺    | 运营  |
|  4 | 丁彩霞  | 销售  |
|  5 | 丁思涵  | 销售  |
| 12 | 付晓杰  | 销售  |
+----+---------+-------+
12 rows in set (0.00 sec)

mysql> select info.id,info.name,depart.title from info right outer join depart on info.depart_id=depart.id;
+------+---------+-------+
| id   | name    | title |
+------+---------+-------+
|    1 | JackHao | 开发  |
|    2 | jomi    | 开发  |
|    3 | 丁佳洪  | 运营  |
|    4 | 丁彩霞  | 销售  |
|    5 | 丁思涵  | 销售  |
|    6 | 丁洁    | 运营  |
|    7 | 丁贤源  | 开发  |
|    8 | 么雪松  | 运营  |
|    9 | 于姗姗  | 运营  |
|   10 | 于玺    | 运营  |
|   11 | 付强    | 开发  |
|   12 | 付晓杰  | 销售  |
+------+---------+-------+
12 rows in set (0.00 sec)

简写:select * from info left join depart on depart.id =info.depart_id;

内连接

只会输出两种表都有数据的数据,如果输出的列有一列是null,就不输出

select info.id,info.name,depart.title from info inner join depart on info.depart_id=depart.id;
mysql> select info.id,info.name,depart.title from info inner join depart on info.depart_id=depart.id;
+----+---------+-------+
| id | name    | title |
+----+---------+-------+
|  1 | JackHao | 开发  |
|  2 | jomi    | 开发  |
|  3 | 丁佳洪  | 运营  |
|  4 | 丁彩霞  | 销售  |
|  5 | 丁思涵  | 销售  |
|  6 | 丁洁    | 运营  |
|  7 | 丁贤源  | 开发  |
|  8 | 么雪松  | 运营  |
|  9 | 于姗姗  | 运营  |
| 10 | 于玺    | 运营  |
| 11 | 付强    | 开发  |
| 12 | 付晓杰  | 销售  |
+----+---------+-------+
12 rows in set (0.00 sec)

上下连表

表 union 表; -- 上下链接,但是需要列数相同,列的类型可以不同,并且具有自动去重的功能
-- 上下连接表的方式
mysql> select id,name from info union select * from depart;
+----+---------+
| id | name    |
+----+---------+
|  1 | JackHao |
|  2 | jomi    |
|  3 | 丁佳洪  |
|  4 | 丁彩霞  |
|  5 | 丁思涵  |
|  6 | 丁洁    |
|  7 | 丁贤源  |
|  8 | 么雪松  |
|  9 | 于姗姗  |
| 10 | 于玺    |
| 11 | 付强    |
| 12 | 付晓杰  |
|  1 | 开发    |
|  2 | 运营    |
|  3 | 销售    |
|  4 | 营销    |
+----+---------+
16 rows in set (0.00 sec)
-- 具有自动去重功能
mysql> select id from info union select id from depart;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
|  7 |
|  8 |
|  9 |
| 10 |
| 11 |
| 12 |
+----+
12 rows in set (0.00 sec)
-- 保留所有数据
mysql> select id from info union all select id from depart;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
|  4 |
|  5 |
|  6 |
|  7 |
|  8 |
|  9 |
| 10 |
| 11 |
| 12 |
|  1 |
|  2 |
|  3 |
|  4 |
+----+
16 rows in set (0.00 sec)

最后我们的mysql的查询顺序

from > join > on > where > group by > having > select > order by > limit

  • 21
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿龙的代码在报错

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值