MYSQL回顾(单表查询相关)

Let's embrace 2020 for realizing our dreams and living a better year.
更好2020,让梦想落地,让更好发生。

数据准备

建表

mysql> create table employee(

    -> id int primary key auto_increment,

    -> name char(40),

    -> age int default 18,

    -> sex enum("male", "female") not null default "male",

    -> position char(20),

    -> salary float default 3000,                                                                  

    -> dep_id int                                                                                 

    -> );                                                                                      

Query OK, 0 rows affected (0.03 sec)

mysql> desc employee;

+----------+-----------------------+------+-----+---------+----------------+

| Field    | Type                  | Null | Key | Default | Extra          |

+----------+-----------------------+------+-----+---------+----------------+

| id       | int(11)               | NO   | PRI | NULL    | auto_increment |

| name     | char(40)              | YES  |     | NULL    |                |

| age      | int(11)               | YES  |     | 18      |                |

| sex      | enum('male','female') | NO   |     | male    |                |

| position | char(20)              | YES  |     | NULL    |                |

| salary   | float                 | YES  |     | 3000    |                |

| dep_id   | int(11)               | YES  |     | NULL    |                |

+----------+-----------------------+------+-----+---------+----------------+

7 rows in set (0.00 sec)

插入数据

mysql> insert into employee(name, age, sex, position, salary, dep_id) values

    -> ("jack", 20, "male", "lawyer", 888888.8, 3),

    -> ("mark", 22, "male", "lawyer", 888888.8, 3),

    -> ("hank", 25, "male", "lawyer", 7777.8, 3),

    -> ("nick", 39, "male", "lawyer", 4438888.8, 3),

    -> ("jenny", 26, "female", "lawyer", 10000.8, 3),

    -> ("tony", 35, "male", "RD", 99999999, 1),

    -> ("emmy", 27, "female", "RD", 9999, 1),

    -> ("emmy", 23, "female", "finance", 5000, 2),

    -> ("lucy", 45, "female", "finance", 10000, 2)

    -> ;

Query OK, 9 rows affected (0.01 sec)

Records: 9  Duplicates: 0  Warnings: 0

Where 查询

mysql> select name from employee where age > 30;

+------+

| name |

+------+

| nick |

| tony |

| lucy |

+------+

3 rows in set (0.01 sec)

group by查询

mysql> select * from employee group by dep_id;

ERROR 1055 (42000): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'alpha.employee.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

原因:因为group by分组之后不能访问分组字段之外的字段,所以以下的select * 会报错. 换成 select dep_id from employee group by dep_id;

但是又无意义,所以此时需要用到聚合函数或者group_concat()函数

聚合函数

聚合函数聚合的是组的内容,若是没有分组,则默认是一组。聚合函数有:
count():取个数
max():取最大值
min():取最小值
avg():取平均值
sum():求和
例如:

    SELECT COUNT(*) FROM employee;
    SELECT COUNT(*) FROM employee WHERE depart_id=1;
    SELECT MAX(salary) FROM employee;
    SELECT MIN(salary) FROM employee;
    SELECT AVG(salary) FROM employee;
    SELECT SUM(salary) FROM employee;
    SELECT SUM(salary) FROM employee WHERE depart_id=3;</pre>
mysql> select count(dep_id), dep_id from employee group by dep_id;

+---------------+--------+

| count(dep_id) | dep_id |

+---------------+--------+

|             5 |      3 |

|             2 |      1 |

|             2 |      2 |

+---------------+--------+

3 rows in set (0.00 sec)

如果觉得count(dep_id)展示不友好,可以使用as关键字给该字段起别名

mysql> select count(dep_id) as dep_id_count, dep_id from employee group by dep_id;

+--------------+--------+

| dep_id_count | dep_id |

+--------------+--------+

|            5 |      3 |

|            2 |      1 |

|            2 |      2 |

+--------------+--------+

3 rows in set (0.00 sec)

Where + group by查询

mysql> select count(name), dep_id from employee where salary > 5000 group by dep_id;

+-------------+--------+

| count(name) | dep_id |

+-------------+--------+

|           5 |      3 |

|           2 |      1 |

|           1 |      2 |

+-------------+--------+

3 rows in set (0.00 sec)

Having查询

查询各部门员工个数小于3的部门id、部门员工姓名、员工个数

mysql> select dep_id, group_concat(name), count(id) from employee group by dep_id having count(id) < 3;

+--------+--------------------+-----------+

| dep_id | group_concat(name) | count(id) |

+--------+--------------------+-----------+

|      1 | tony,emmy          |         2 |

|      2 | emmy,lucy          |         2 |

+--------+--------------------+-----------+

2 rows in set (0.00 sec)

查询各部门平均工资大于10000的部门id、部门平均工资

mysql> select group_concat(dep_id), avg(salary) from employee group by dep_id having avg(salary) > 10000;

+----------------------+-------------------+

| group_concat(dep_id) | avg(salary)       |

+----------------------+-------------------+

| 1,1                  |        50004999.5 |

| 3,3,3,3,3            | 1246889.044921875 |

+----------------------+-------------------+

2 rows in set (0.00 sec)

查询各部门平局工资大于10000且小于10000000的部门id、部门平均工资

mysql> select group_concat(dep_id), avg(salary) from employee group by dep_id having avg(salary) > 10000 and avg(salary) < 10000000;

+----------------------+-------------------+

| group_concat(dep_id) | avg(salary)       |

+----------------------+-------------------+

| 3,3,3,3,3            | 1246889.044921875 |

+----------------------+-------------------+

1 row in set (0.00 sec)

HAVING与WHERE不一样的地方在于!!!!!!

!!!执行优先级从高到低:where > group by > having

1. Where 发生在分组group by之前,因而Where中可以有任意字段,但是绝对不能使用聚合函数。

2. Having发生在分组group by之后,因而Having中可以使用分组的字段,无法直接取到其他字段,可以使用聚合函数

order by查询

排序分为升序ASC和降序DESC

mysql> select * from employee order by salary ASC;

+----+-------+------+--------+----------+-----------+--------+

| id | name  | age  | sex    | position | salary    | dep_id |

+----+-------+------+--------+----------+-----------+--------+

|  8 | emmy  |   23 | female | finance  |      5000 |      2 |

|  3 | hank  |   25 | male   | lawyer   |    7777.8 |      3 |

|  7 | emmy  |   27 | female | RD       |      9999 |      1 |

|  9 | lucy  |   45 | female | finance  |     10000 |      2 |

|  5 | jenny |   26 | female | lawyer   |   10000.8 |      3 |

|  1 | jack  |   20 | male   | lawyer   |    888889 |      3 |

|  2 | mark  |   22 | male   | lawyer   |    888889 |      3 |

|  4 | nick  |   39 | male   | lawyer   |   4438890 |      3 |

|  6 | tony  |   35 | male   | RD       | 100000000 |      1 |

+----+-------+------+--------+----------+-----------+--------+

9 rows in set (0.00 sec)
mysql> select * from employee order by salary DESC;

+----+-------+------+--------+----------+-----------+--------+

| id | name  | age  | sex    | position | salary    | dep_id |

+----+-------+------+--------+----------+-----------+--------+

|  6 | tony  |   35 | male   | RD       | 100000000 |      1 |

|  4 | nick  |   39 | male   | lawyer   |   4438890 |      3 |

|  1 | jack  |   20 | male   | lawyer   |    888889 |      3 |

|  2 | mark  |   22 | male   | lawyer   |    888889 |      3 |

|  5 | jenny |   26 | female | lawyer   |   10000.8 |      3 |

|  9 | lucy  |   45 | female | finance  |     10000 |      2 |

|  7 | emmy  |   27 | female | RD       |      9999 |      1 |

|  3 | hank  |   25 | male   | lawyer   |    7777.8 |      3 |

|  8 | emmy  |   23 | female | finance  |      5000 |      2 |

+----+-------+------+--------+----------+-----------+--------+

9 rows in set (0.00 sec)

查询所有员工信息,先按照部门id升序,如果id相同则按照年龄降序

mysql> select * from employee order by dep_id ASC, age DESC;

+----+-------+------+--------+----------+-----------+--------+

| id | name  | age  | sex    | position | salary    | dep_id |

+----+-------+------+--------+----------+-----------+--------+

|  6 | tony  |   35 | male   | RD       | 100000000 |      1 |

|  7 | emmy  |   27 | female | RD       |      9999 |      1 |

|  9 | lucy  |   45 | female | finance  |     10000 |      2 |

|  8 | emmy  |   23 | female | finance  |      5000 |      2 |

|  4 | nick  |   39 | male   | lawyer   |   4438890 |      3 |

|  5 | jenny |   26 | female | lawyer   |   10000.8 |      3 |

|  3 | hank  |   25 | male   | lawyer   |    7777.8 |      3 |

|  2 | mark  |   22 | male   | lawyer   |    888889 |      3 |

|  1 | jack  |   20 | male   | lawyer   |    888889 |      3 |

+----+-------+------+--------+----------+-----------+--------+

9 rows in set (0.00 sec)

查询各部门平均工资大于100000的部门id、平均工资,结果按平均工资升序

mysql> select dep_id, avg(salary) from employee group by dep_id having avg(salary) > 100000 order by avg(salary) ASC;

+--------+-------------------+

| dep_id | avg(salary)       |

+--------+-------------------+

|      3 | 1246889.044921875 |

|      1 |        50004999.5 |

+--------+-------------------+

2 rows in set (0.00 sec)

limit查询

limit限制查询的记录条数

查询工资大于10000的 的前三名员工信息,并按降序排列

mysql> select * from employee where salary > 10000 order by salary DESC limit 3;

+----+------+------+------+----------+-----------+--------+

| id | name | age  | sex  | position | salary    | dep_id |

+----+------+------+------+----------+-----------+--------+

|  6 | tony |   35 | male | RD       | 100000000 |      1 |

|  4 | nick |   39 | male | lawyer   |   4438890 |      3 |

|  1 | jack |   20 | male | lawyer   |    888889 |      3 |

+----+------+------+------+----------+-----------+--------+

3 rows in set (0.00 sec)

limit分页查询

每页3条,查询第一页:

mysql> select * from employee order by salary limit 0, 3;

+----+------+------+--------+----------+--------+--------+

| id | name | age  | sex    | position | salary | dep_id |

+----+------+------+--------+----------+--------+--------+

|  8 | emmy |   23 | female | finance  |   5000 |      2 |

|  3 | hank |   25 | male   | lawyer   | 7777.8 |      3 |

|  7 | emmy |   27 | female | RD       |   9999 |      1 |

+----+------+------+--------+----------+--------+--------+

3 rows in set (0.00 sec)

每页三条,查询第二页:

mysql> select * from employee order by salary limit 3, 3;

+----+-------+------+--------+----------+---------+--------+

| id | name  | age  | sex    | position | salary  | dep_id |

+----+-------+------+--------+----------+---------+--------+

|  9 | lucy  |   45 | female | finance  |   10000 |      2 |

|  5 | jenny |   26 | female | lawyer   | 10000.8 |      3 |

|  1 | jack  |   20 | male   | lawyer   |  888889 |      3 |

+----+-------+------+--------+----------+---------+--------+

3 rows in set (0.01 sec)

每页3条,查询第三页:

mysql> select * from employee order by salary limit 6, 3;

+----+------+------+------+----------+-----------+--------+

| id | name | age  | sex  | position | salary    | dep_id |

+----+------+------+------+----------+-----------+--------+

|  2 | mark |   22 | male | lawyer   |    888889 |      3 |

|  4 | nick |   39 | male | lawyer   |   4438890 |      3 |

|  6 | tony |   35 | male | RD       | 100000000 |      1 |

+----+------+------+------+----------+-----------+--------+

3 rows in set (0.00 sec)

正则表达式查询

查询所有员工中以em开头的员工信息:

^代表开头

mysql> select * from employee where name REGEXP '^em';

+----+------+------+--------+----------+--------+--------+

| id | name | age  | sex    | position | salary | dep_id |

+----+------+------+--------+----------+--------+--------+

|  7 | emmy |   27 | female | RD       |   9999 |      1 |

|  8 | emmy |   23 | female | finance  |   5000 |      2 |

+----+------+------+--------+----------+--------+--------+

2 rows in set (0.00 sec)

查询所有员工中以ck结尾的员工信息:

$代表结尾

mysql> select * from employee where name REGEXP 'ck$';

+----+------+------+------+----------+---------+--------+

| id | name | age  | sex  | position | salary  | dep_id |

+----+------+------+------+----------+---------+--------+

|  1 | jack |   20 | male | lawyer   |  888889 |      3 |

|  4 | nick |   39 | male | lawyer   | 4438890 |      3 |

+----+------+------+------+----------+---------+--------+

2 rows in set (0.00 sec)

查询所有员工姓名包含2个连续m的员工信息:

mysql> SELECT * FROM employee WHERE name REGEXP 'm{2}';

+----+------+------+--------+----------+--------+--------+

| id | name | age  | sex    | position | salary | dep_id |

+----+------+------+--------+----------+--------+--------+

|  7 | emmy |   27 | female | RD       |   9999 |      1 |

|  8 | emmy |   23 | female | finance  |   5000 |      2 |

+----+------+------+--------+----------+--------+--------+

2 rows in set (0.00 sec)

查询所有员工中姓名以emm开头且已y结尾的员工信息:

mysql> select * from employee where name regexp '^emm.*[y]$’;

+----+------+------+--------+----------+--------+--------+

| id | name | age  | sex    | position | salary | dep_id |

+----+------+------+--------+----------+--------+--------+

|  7 | emmy |   27 | female | RD       |   9999 |      1 |

|  8 | emmy |   23 | female | finance  |   5000 |      2 |

+----+------+------+--------+----------+--------+--------+

2 rows in set (0.00 sec)

查询所有员工中姓名以emm开头且已i或y结尾的员工信息:

mysql> select * from employee where name regexp '^emm.*[iy]$';

+----+------+------+--------+----------+--------+--------+

| id | name | age  | sex    | position | salary | dep_id |

+----+------+------+--------+----------+--------+--------+

|  7 | emmy |   27 | female | RD       |   9999 |      1 |

|  8 | emmy |   23 | female | finance  |   5000 |      2 |

| 10 | emmi |   20 | female | finance  |  20000 |      2 |

+----+------+------+--------+----------+--------+--------+

3 rows in set (0.00 sec)

另外还有一个模糊查询:like 但是like只有下划线_和百分号%

Like关键字模糊匹配姓名以emm开头的记录

mysql> select * from employee where name like 'emm%';

+----+------+------+--------+----------+--------+--------+

| id | name | age  | sex    | position | salary | dep_id |

+----+------+------+--------+----------+--------+--------+

|  7 | emmy |   27 | female | RD       |   9999 |      1 |

|  8 | emmy |   23 | female | finance  |   5000 |      2 |

| 10 | emmi |   20 | female | finance  |  20000 |      2 |

+----+------+------+--------+----------+--------+--------+

3 rows in set (0.00 sec)

关键字执行顺序

重点中的重点:单表查询关键字的执行顺序(优先级)

from

where

group by

having

select

distinct

order by

limit

image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,233评论 6 495
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,357评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,831评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,313评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,417评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,470评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,482评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,265评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,708评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,997评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,176评论 1 342
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,827评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,503评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,150评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,391评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,034评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,063评论 2 352