mysql学习一之select语句

mysql学习一之select语句

    • 完整语法
    • 创建User 表
    • 单表查询
    • 查询空值
    • AND多条件查询
    • OR 多条件查询
    • DISTINCT
    • 查询结果排序
    • 分组查询

完整语法

先给一下完整的语法,后面将逐一来讲解。

  • 基础语法:select 字段列表 from 数据源;

  • 完整语法:

select 去重选项 字段列表 [as 字段别名] 
from 数据源 
[where子句] 
[group by 子句] 
[having子句] 
[order by 子句] 
[limit子句];

创建User 表

  • 创建User表
use test;
CREATE TABLE `test`.`user` (
  `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(32) NOT NULL,
  `pwd` VARCHAR(32) NOT NULL,
  `age` INT NOT NULL,
  `sex` VARCHAR(8) NOT NULL,
  PRIMARY KEY (`id`));

  • 插入数据
use test;
insert into user(name,pwd,age,sex) 
values ('小灰灰','xiaohuihui',6,'男'),('黑太郎','heitailang',20,'男');
  • 查询数据
./mysql -h 127.0.0.1 -u root -P 3306 -p 
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select id,name from user;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | 喜羊羊    |
|  2 | 懒羊羊    |
|  3 | 慢羊羊    |
|  4 | 慢羊羊    |
|  7 | 沸羊羊    |
|  8 | 花羊羊    |
|  9 | 奔羊羊    |
| 10 | 瘦羊羊    |
| 11 | 红太狼    |
| 12 | 蕉太狼    |
| 13 | 小灰灰    |
| 14 | 黑太郎    |
+----+-----------+
12 rows in set (0.00 sec)

单表查询

  • 在SELECT 语句中使用 “*” 通配符查询所有字段
mysql> SELECT * FROM user;
+----+-----------+-------------+-----+-----+
| id | name      | pwd         | age | sex |
+----+-----------+-------------+-----+-----+
|  1 | 喜羊羊    | xiyangyang  |  12 ||
|  2 | 懒羊羊    | lanyangyang |  10 ||
|  3 | 慢羊羊    | meiyangyang |  10 ||
|  4 | 慢羊羊    | manyangyang |  14 ||
|  7 | 沸羊羊    | feiyangyang |  16 ||
|  8 | 花羊羊    | huayangyang |  18 ||
|  9 | 奔羊羊    | benyangyang |  20 ||
| 10 | 瘦羊羊    | souyangyang |  24 ||
| 11 | 红太狼    | hongtailang |  20 ||
| 12 | 蕉太狼    | jiaotailang |  24 ||
| 13 | 小灰灰    | xiaohuihui  |   6 ||
| 14 | 黑太郎    | heitailang  |  20 ||
+----+-----------+-------------+-----+-----+
12 rows in set (0.00 sec)
  • 在SELECT 语句中指定所有字段
mysql> SELECT id,name,pwd,age,sex FROM user;
+----+-----------+-------------+-----+-----+
| id | name      | pwd         | age | sex |
+----+-----------+-------------+-----+-----+
|  1 | 喜羊羊    | xiyangyang  |  12 ||
|  2 | 懒羊羊    | lanyangyang |  10 ||
|  3 | 慢羊羊    | meiyangyang |  10 ||
|  4 | 慢羊羊    | manyangyang |  14 ||
|  7 | 沸羊羊    | feiyangyang |  16 ||
|  8 | 花羊羊    | huayangyang |  18 ||
|  9 | 奔羊羊    | benyangyang |  20 ||
| 10 | 瘦羊羊    | souyangyang |  24 ||
| 11 | 红太狼    | hongtailang |  20 ||
| 12 | 蕉太狼    | jiaotailang |  24 ||
| 13 | 小灰灰    | xiaohuihui  |   6 ||
| 14 | 黑太郎    | heitailang  |  20 ||
+----+-----------+-------------+-----+-----+
12 rows in set (0.00 sec)
  • 查询指定字段:SELECT 列名 FROM 表名;
mysql> SELECT name from user;
+-----------+
| name      |
+-----------+
| 喜羊羊    |
| 懒羊羊    |
| 慢羊羊    |
| 慢羊羊    |
| 沸羊羊    |
| 花羊羊    |
| 奔羊羊    |
| 瘦羊羊    |
| 红太狼    |
| 蕉太狼    |
| 小灰灰    |
| 黑太郎    |
+-----------+
12 rows in set (0.00 sec)
  • 查询多个字段:SELECT 字段名1,字段名2, … 字段名n FROM 表名;
mysql> SELECT name pwd,sex from user;
+-----------+-----+
| pwd       | sex |
+-----------+-----+
| 喜羊羊    ||
| 懒羊羊    ||
| 慢羊羊    ||
| 慢羊羊    ||
| 沸羊羊    ||
| 花羊羊    ||
| 奔羊羊    ||
| 瘦羊羊    ||
| 红太狼    ||
| 蕉太狼    ||
| 小灰灰    ||
| 黑太郎    ||
+-----------+-----+
12 rows in set (0.00 sec)
  • 查询指定记录 SELECT 字段名1,字段名2, … 字段名n FROM 表名 WHERE 查询条件
mysql> SELECT name,pwd,sex from user where sex='女';
+-----------+-------------+-----+
| name      | pwd         | sex |
+-----------+-------------+-----+
| 慢羊羊    | meiyangyang ||
| 沸羊羊    | feiyangyang ||
| 花羊羊    | huayangyang ||
| 瘦羊羊    | souyangyang ||
| 红太狼    | hongtailang ||
+-----------+-------------+-----+
5 rows in set (0.01 sec)
  • 查询年龄小于20的记录
mysql> SELECT name,age FROM user where age <20;
+-----------+-----+
| name      | age |
+-----------+-----+
| 喜羊羊    |  12 |
| 懒羊羊    |  10 |
| 慢羊羊    |  10 |
| 慢羊羊    |  14 |
| 沸羊羊    |  16 |
| 花羊羊    |  18 |
| 小灰灰    |   6 |
+-----------+-----+
7 rows in set (0.01 sec)
  • IN 关键字查询
mysql> SELECT id,name,sex FROM user  where id in (1,2,3,4);
+----+-----------+-----+
| id | name      | sex |
+----+-----------+-----+
|  1 | 喜羊羊    ||
|  2 | 懒羊羊    ||
|  3 | 慢羊羊    ||
|  4 | 慢羊羊    ||
+----+-----------+-----+
4 rows in set (0.00 sec)
  • NOT IN 关键字查询
mysql> SELECT id,name,sex FROM user  where id not in (1,2,3,4);
+----+-----------+-----+
| id | name      | sex |
+----+-----------+-----+
|  7 | 沸羊羊    ||
|  8 | 花羊羊    ||
|  9 | 奔羊羊    ||
| 10 | 瘦羊羊    ||
| 11 | 红太狼    ||
| 12 | 蕉太狼    ||
| 13 | 小灰灰    ||
| 14 | 黑太郎    ||
+----+-----------+-----+
8 rows in set (0.01 sec)
  • BETWEEN AND 关键字查询
mysql> SELECT id,name,sex,age FROM user where age between 20 and 24;
+----+-----------+-----+-----+
| id | name      | sex | age |
+----+-----------+-----+-----+
|  9 | 奔羊羊    ||  20 |
| 10 | 瘦羊羊    ||  24 |
| 11 | 红太狼    ||  20 |
| 12 | 蕉太狼    ||  24 |
| 14 | 黑太郎    ||  20 |
+----+-----------+-----+-----+
5 rows in set (0.00 sec)
  • NOT BETWEEN AND 关键字查询
mysql> SELECT id,name,sex,age FROM user where age not between 20 and 24;
+----+-----------+-----+-----+
| id | name      | sex | age |
+----+-----------+-----+-----+
|  1 | 喜羊羊    ||  12 |
|  2 | 懒羊羊    ||  10 |
|  3 | 慢羊羊    ||  10 |
|  4 | 慢羊羊    ||  14 |
|  7 | 沸羊羊    ||  16 |
|  8 | 花羊羊    ||  18 |
| 13 | 小灰灰    ||   6 |
+----+-----------+-----+-----+
7 rows in set (0.00 sec)
  • 查询以“小”开头的数据
mysql> SELECT id,name,age,sex FROM user where name like '小%';
+----+-----------+-----+-----+
| id | name      | age | sex |
+----+-----------+-----+-----+
| 13 | 小灰灰    |   6 ||
+----+-----------+-----+-----+
1 row in set (0.00 sec)
  • 查询以“喜”开头 和 “ 羊”结尾的数据
mysql> SELECT id,name,age,sex FROM user where name like '喜%羊';
+----+-----------+-----+-----+
| id | name      | age | sex |
+----+-----------+-----+-----+
|  1 | 喜羊羊    |  12 ||
+----+-----------+-----+-----+
1 row in set (0.00 sec)
  • 下划线通配符查询
mysql> SELECT id,name,age,sex FROM user where name like '__灰';
+----+-----------+-----+-----+
| id | name      | age | sex |
+----+-----------+-----+-----+
| 13 | 小灰灰    |   6 ||
+----+-----------+-----+-----+
1 row in set (0.00 sec)

查询空值

  • 添加字段email
ALTER TABLE `test`.`user` 
ADD COLUMN `email` VARCHAR(64) NULL AFTER `sex`;
UPDATE `test`.`user` SET `email` = '[email protected]' WHERE (`id` = '1');
UPDATE `test`.`user` SET `email` = '[email protected]' WHERE (`id` = '2');
UPDATE `test`.`user` SET `email` = '[email protected]' WHERE (`id` = '3');
UPDATE `test`.`user` SET `email` = '[email protected]' WHERE (`id` = '4');
UPDATE `test`.`user` SET `email` = '[email protected]' WHERE (`id` = '7');
  • IS NULL 查询
mysql> SELECT id,name,age,sex,email FROM user WHERE email IS NULL;
+----+-----------+-----+-----+-------+
| id | name      | age | sex | email |
+----+-----------+-----+-----+-------+
|  8 | 花羊羊    |  18 || NULL  |
|  9 | 奔羊羊    |  20 || NULL  |
| 10 | 瘦羊羊    |  24 || NULL  |
| 11 | 红太狼    |  20 || NULL  |
| 12 | 蕉太狼    |  24 || NULL  |
| 13 | 小灰灰    |   6 || NULL  |
| 14 | 黑太郎    |  20 || NULL  |
+----+-----------+-----+-----+-------+
7 rows in set (0.00 sec)
  • IS NOT NULL 查询
mysql> SELECT id,name,age,sex,email FROM user WHERE email IS NOT NULL;
+----+-----------+-----+-----+---------------------+
| id | name      | age | sex | email               |
+----+-----------+-----+-----+---------------------+
|  1 | 喜羊羊    |  12 || xiyangyang@163.com  |
|  2 | 懒羊羊    |  10 || lanyangyang@163.com |
|  3 | 慢羊羊    |  10 || meiyangyang@163.com |
|  4 | 慢羊羊    |  14 || manyangyang@163.com |
|  7 | 沸羊羊    |  16 || feiyangyang@163.com |
+----+-----------+-----+-----+---------------------+
5 rows in set (0.00 sec)

AND多条件查询

mysql> SELECT id,name,age,sex,email FROM user where id IN (4,7) AND age = 16 and name=' 沸羊羊';
+----+-----------+-----+-----+---------------------+
| id | name      | age | sex | email               |
+----+-----------+-----+-----+---------------------+
|  7 | 沸羊羊    |  16 || feiyangyang@163.com |
+----+-----------+-----+-----+---------------------+
1 row in set (0.00 sec)

OR 多条件查询

mysql> SELECT id,name,age,sex,email FROM user where id=4 OR id=7;
+----+-----------+-----+-----+---------------------+
| id | name      | age | sex | email               |
+----+-----------+-----+-----+---------------------+
|  4 | 慢羊羊    |  14 || manyangyang@163.com |
|  7 | 沸羊羊    |  16 || feiyangyang@163.com |
+----+-----------+-----+-----+---------------------+
2 rows in set (0.00 sec)

DISTINCT

  • 插入一条数据
INSERT INTO `test`.`user` (`name`, `pwd`, `age`, `sex`, `email`) 
VALUES ('喜羊羊', 'xiyangyang', '12', '男', '[email protected]');
  • 不使用 distinct 关键字
mysql> SELECT  name FROM user;
+-----------+
| name      |
+-----------+
| 喜羊羊    |
| 懒羊羊    |
| 慢羊羊    |
| 慢羊羊    |
| 沸羊羊    |
| 花羊羊    |
| 奔羊羊    |
| 瘦羊羊    |
| 红太狼    |
| 蕉太狼    |
| 小灰灰    |
| 黑太郎    |
| 喜羊羊    |
+-----------+
13 rows in set (0.00 sec)
  • 使用 distinct 关键字
mysql> SELECT distinct name FROM user;
+-----------+
| name      |
+-----------+
| 喜羊羊    |
| 懒羊羊    |
| 慢羊羊    |
| 沸羊羊    |
| 花羊羊    |
| 奔羊羊    |
| 瘦羊羊    |
| 红太狼    |
| 蕉太狼    |
| 小灰灰    |
| 黑太郎    |
+-----------+
11 rows in set (0.00 sec)

查询结果排序

  • 不使用 ORDER BY : 根据数据插入到数据表的顺序来显示
mysql> SELECT name FROM user;
+-----------+
| name      |
+-----------+
| 喜羊羊    |
| 懒羊羊    |
| 慢羊羊    |
| 慢羊羊    |
| 沸羊羊    |
| 花羊羊    |
| 奔羊羊    |
| 瘦羊羊    |
| 红太狼    |
| 蕉太狼    |
| 小灰灰    |
| 黑太郎    |
| 喜羊羊    |
+-----------+
13 rows in set (0.00 sec)
  • 使用 ORDER BY
mysql> SELECT name FROM user ORDER BY name;
+-----------+
| name      |
+-----------+
| 喜羊羊    |
| 喜羊羊    |
| 奔羊羊    |
| 小灰灰    |
| 慢羊羊    |
| 慢羊羊    |
| 懒羊羊    |
| 沸羊羊    |
| 瘦羊羊    |
| 红太狼    |
| 花羊羊    |
| 蕉太狼    |
| 黑太郎    |
+-----------+
13 rows in set (0.00 sec)
  • 使用DESC 关键字
mysql> SELECT name FROM user ORDER BY name DESC;
+-----------+
| name      |
+-----------+
| 黑太郎    |
| 蕉太狼    |
| 花羊羊    |
| 红太狼    |
| 瘦羊羊    |
| 沸羊羊    |
| 懒羊羊    |
| 慢羊羊    |
| 慢羊羊    |
| 小灰灰    |
| 奔羊羊    |
| 喜羊羊    |
| 喜羊羊    |
+-----------+
13 rows in set (0.00 sec)

分组查询

  • 使用 GROUP BY 关键
mysql> SELECT sex,COUNT(*) FROM user GROUP BY sex;
+-----+----------+
| sex | COUNT(*) |
+-----+----------+
||        8 |
||        5 |
+-----+----------+
2 rows in set (0.01 sec)
  • 使用GROUP_CONCAT函数
mysql> SELECT sex,GROUP_CONCAT(name)  FROM user GROUP BY sex;
+-----+---------------------------------------------------------------------------------+
| sex | GROUP_CONCAT(name)                                                              |
+-----+---------------------------------------------------------------------------------+
|| 慢羊羊,沸羊羊,花羊羊,瘦羊羊,红太狼                                              |
|| 喜羊羊,懒羊羊,慢羊羊,奔羊羊,蕉太狼,小灰灰,黑太郎,喜羊羊                         |
+-----+---------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
  • 使用HAVING
mysql> SELECT sex,GROUP_CONCAT(name) as names  FROM user GROUP BY sex HAVING COUNT(name) > 5;
+-----+---------------------------------------------------------------------------------+
| sex | names                                                                           |
+-----+---------------------------------------------------------------------------------+
|| 喜羊羊,懒羊羊,慢羊羊,奔羊羊,蕉太狼,小灰灰,黑太郎,喜羊羊                         |
+-----+---------------------------------------------------------------------------------+
1 row in set (0.00 sec)
  • 使用WITH ROLLUP
mysql> SELECT sex,GROUP_CONCAT(name)  FROM user GROUP BY sex WITH ROLLUP;
+------+-----------------------------------------------------------------------------------------------------------------------------------+
| sex  | GROUP_CONCAT(name)                                                                                                                |
+------+-----------------------------------------------------------------------------------------------------------------------------------+
|| 慢羊羊,沸羊羊,花羊羊,瘦羊羊,红太狼                                                                                                |
|| 喜羊羊,懒羊羊,慢羊羊,奔羊羊,蕉太狼,小灰灰,黑太郎,喜羊羊                                                                           |
| NULL | 慢羊羊,沸羊羊,花羊羊,瘦羊羊,红太狼,喜羊羊,懒羊羊,慢羊羊,奔羊羊,蕉太狼,小灰灰,黑太郎,喜羊羊                                        |
+------+-----------------------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.01 sec)
mysql> SELECT sex,COUNT(name)  FROM user GROUP BY sex WITH ROLLUP;
+------+-------------+
| sex  | COUNT(name) |
+------+-------------+
||           5 |
||           8 |
| NULL |          13 |
+------+-------------+
3 rows in set (0.00 sec)

你可能感兴趣的:(mysql)