先给一下完整的语法,后面将逐一来讲解。
基础语法:select 字段列表 from 数据源;
完整语法:
select 去重选项 字段列表 [as 字段别名]
from 数据源
[where子句]
[group by 子句]
[having子句]
[order by 子句]
[limit子句];
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)
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)
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)
mysql> SELECT name from user;
+-----------+
| name |
+-----------+
| 喜羊羊 |
| 懒羊羊 |
| 慢羊羊 |
| 慢羊羊 |
| 沸羊羊 |
| 花羊羊 |
| 奔羊羊 |
| 瘦羊羊 |
| 红太狼 |
| 蕉太狼 |
| 小灰灰 |
| 黑太郎 |
+-----------+
12 rows in set (0.00 sec)
mysql> SELECT name pwd,sex from user;
+-----------+-----+
| pwd | sex |
+-----------+-----+
| 喜羊羊 | 男 |
| 懒羊羊 | 男 |
| 慢羊羊 | 女 |
| 慢羊羊 | 男 |
| 沸羊羊 | 女 |
| 花羊羊 | 女 |
| 奔羊羊 | 男 |
| 瘦羊羊 | 女 |
| 红太狼 | 女 |
| 蕉太狼 | 男 |
| 小灰灰 | 男 |
| 黑太郎 | 男 |
+-----------+-----+
12 rows in set (0.00 sec)
mysql> SELECT name,pwd,sex from user where sex='女';
+-----------+-------------+-----+
| name | pwd | sex |
+-----------+-------------+-----+
| 慢羊羊 | meiyangyang | 女 |
| 沸羊羊 | feiyangyang | 女 |
| 花羊羊 | huayangyang | 女 |
| 瘦羊羊 | souyangyang | 女 |
| 红太狼 | hongtailang | 女 |
+-----------+-------------+-----+
5 rows in set (0.01 sec)
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)
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)
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)
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)
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)
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');
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)
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)
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)
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)
INSERT INTO `test`.`user` (`name`, `pwd`, `age`, `sex`, `email`)
VALUES ('喜羊羊', 'xiyangyang', '12', '男', '[email protected]');
mysql> SELECT name FROM user;
+-----------+
| name |
+-----------+
| 喜羊羊 |
| 懒羊羊 |
| 慢羊羊 |
| 慢羊羊 |
| 沸羊羊 |
| 花羊羊 |
| 奔羊羊 |
| 瘦羊羊 |
| 红太狼 |
| 蕉太狼 |
| 小灰灰 |
| 黑太郎 |
| 喜羊羊 |
+-----------+
13 rows in set (0.00 sec)
mysql> SELECT distinct name FROM user;
+-----------+
| name |
+-----------+
| 喜羊羊 |
| 懒羊羊 |
| 慢羊羊 |
| 沸羊羊 |
| 花羊羊 |
| 奔羊羊 |
| 瘦羊羊 |
| 红太狼 |
| 蕉太狼 |
| 小灰灰 |
| 黑太郎 |
+-----------+
11 rows in set (0.00 sec)
mysql> SELECT name FROM user;
+-----------+
| name |
+-----------+
| 喜羊羊 |
| 懒羊羊 |
| 慢羊羊 |
| 慢羊羊 |
| 沸羊羊 |
| 花羊羊 |
| 奔羊羊 |
| 瘦羊羊 |
| 红太狼 |
| 蕉太狼 |
| 小灰灰 |
| 黑太郎 |
| 喜羊羊 |
+-----------+
13 rows in set (0.00 sec)
mysql> SELECT name FROM user ORDER BY name;
+-----------+
| name |
+-----------+
| 喜羊羊 |
| 喜羊羊 |
| 奔羊羊 |
| 小灰灰 |
| 慢羊羊 |
| 慢羊羊 |
| 懒羊羊 |
| 沸羊羊 |
| 瘦羊羊 |
| 红太狼 |
| 花羊羊 |
| 蕉太狼 |
| 黑太郎 |
+-----------+
13 rows in set (0.00 sec)
mysql> SELECT name FROM user ORDER BY name DESC;
+-----------+
| name |
+-----------+
| 黑太郎 |
| 蕉太狼 |
| 花羊羊 |
| 红太狼 |
| 瘦羊羊 |
| 沸羊羊 |
| 懒羊羊 |
| 慢羊羊 |
| 慢羊羊 |
| 小灰灰 |
| 奔羊羊 |
| 喜羊羊 |
| 喜羊羊 |
+-----------+
13 rows in set (0.00 sec)
mysql> SELECT sex,COUNT(*) FROM user GROUP BY sex;
+-----+----------+
| sex | COUNT(*) |
+-----+----------+
| 男 | 8 |
| 女 | 5 |
+-----+----------+
2 rows in set (0.01 sec)
mysql> SELECT sex,GROUP_CONCAT(name) FROM user GROUP BY sex;
+-----+---------------------------------------------------------------------------------+
| sex | GROUP_CONCAT(name) |
+-----+---------------------------------------------------------------------------------+
| 女 | 慢羊羊,沸羊羊,花羊羊,瘦羊羊,红太狼 |
| 男 | 喜羊羊,懒羊羊,慢羊羊,奔羊羊,蕉太狼,小灰灰,黑太郎,喜羊羊 |
+-----+---------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
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)
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)