标题: MySQL8.0大师之路:第9章:MySQL教程-9.6 查询数据 [打印本页] 作者: JeeStudy 时间: 2020-4-3 21:47 标题: MySQL8.0大师之路:第9章:MySQL教程-9.6 查询数据 1.查询所有的数据
mysql> select * from student;
+------+----------+------------+
| id | name | birth |
+------+----------+------------+
| 1 | zhangsan | 1997-01-01 |
| 2 | lisi | 2008-09-10 |
+------+----------+------------+
2 rows in set (0.00 sec)
2.查询特定的行:查询名字是lisi的数据
mysql> select * from student where name='lisi';
+------+------+------------+
| id | name | birth |
+------+------+------------+
| 2 | lisi | 2008-09-10 |
+------+------+------------+
1 row in set (0.01 sec)
3.查询固定列,例如,查询出表中的所有名字
mysql> select name from student;
+----------+
| name |
+----------+
| zhangsan |
| lisi |
+----------+
2 rows in set (0.00 sec)
4.排序:根据id倒序排列
mysql> select * from student order by id desc;
+------+----------+------------+
| id | name | birth |
+------+----------+------------+
| 2 | lisi | 2008-09-10 |
| 1 | zhangsan | 1997-01-01 |
+------+----------+------------+
2 rows in set (0.00 sec)
5.日期计算,例如计算每个人的年龄
mysql> select *,TIMESTAMPDIFF(YEAR,birth,CURDATE()) AS age from student;
+------+-----------+--------------+--------+
| id | name | birth | age |
+------+-----------+--------------+--------+
| 1 | zhangsan | 1997-01-01 | 23 |
| 2 | lisi | 2008-09-10 | 11 |
+------+-----------+--------------+--------+
2 rows in set (0.00 sec)
6.查询生日是1月份的人
mysql> select * from student WHERE MONTH(birth) = 1;
+------+------------+--------------+
| id | name | birth |
+------+------------+--------------+
| 1 | zhangsan | 1997-01-01 |
+------+------------+--------------+
1 row in set (0.00 sec)
8.模式匹配(模糊查询)
查询操作符不要用 = or <>,要使用 LIKE or NOT LIKE 1)查询名字中以 l 开头的人
mysql> select * from student where name like 'l%';
+------+------+------------+
| id | name | birth |
+------+------+------------+
| 2 | lisi | 2008-09-10 |
+------+------+------------+
1 row in set (0.01 sec)
2).查询名字中以u结尾的人名
mysql> select * from student where name like '%u';
+------+---------+------------+
| id | name | birth |
+------+---------+------------+
| 3 | wangwu | 1919-05-01 |
| 4 | zhaoliu | 2018-08-10 |
+------+---------+------------+
2 rows in set (0.00 sec)
3).查询名字中包含 g 这个字母的人
mysql> select * from student where name like '%g%';
+------+----------+------------+
| id | name | birth |
+------+----------+------------+
| 1 | zhangsan | 1997-01-01 |
| 3 | wangwu | 1919-05-01 |
+------+----------+------------+
2 rows in set (0.00 sec)
4).查询名字的长度是4位的人
mysql> select * from student where name like '____';
+------+------+------------+
| id | name | birth |
+------+------+------------+
| 2 | lisi | 2008-09-10 |
+------+------+------------+
1 row in set (0.00 sec)
<1>查询名字中以z开头的人
mysql> select * from student WHERE REGEXP_LIKE(name, '^z');
+----+--------------------+--------------+
| id | name | birth |
+------+------------------+--------------+
| 1 | zhangsan | 1997-01-01 |
| 4 | zhaoliu | 2018-08-10 |
| 5 | ZHOUXINGXING | 1956-08-25 |
+------+------------------+--------------+
3 rows in set (0.00 sec)
<2>查询名字中以Z开头的人,并区分大小写
这三种方式都可以
SELECT * FROM student WHERE REGEXP_LIKE(name, '^Z' COLLATE utf8mb4_0900_as_cs);
SELECT * FROM student WHERE REGEXP_LIKE(name, BINARY '^Z');
SELECT * FROM student WHERE REGEXP_LIKE(name, '^Z', 'c');
mysql> SELECT * FROM student WHERE REGEXP_LIKE(name, '^Z', 'c');
+------+-------------------+------------+
| id | name | birth |
+------+-------------------+------------+
| 5 | ZHOUXINGXING | 1956-08-25 |
+------+-------------------+------------+
1 row in set (0.00 sec)
<3>找出以g结尾的人
mysql> SELECT * FROM student WHERE REGEXP_LIKE(name, 'g$');
+------+-------------------+--------------+
| id | name | birth |
+------+-------------------+--------------+
| 5 | ZHOUXINGXING | 1956-08-25 |
| 6 | DASHENG | 1919-12-23 |
+------+-------------------+--------------+
2 rows in set (0.00 sec)
<4>查询名字中包含o的人
mysql> select * from student WHERE REGEXP_LIKE(name, 'o');
+------+-------------------+--------------+
| id | name | birth |
+------+-------------------+--------------+
| 4 | zhaoliu | 2018-08-10 |
| 5 | ZHOUXINGXING | 1956-08-25 |
+------+-------------------+--------------+
2 rows in set (0.00 sec)
<5>查询名字中包含4个字符的人
mysql> select * from student WHERE REGEXP_LIKE(name, '^....$');
+------+---------+--------------+
| id | name | birth |
+------+---------+--------------+
| 2 | lisi | 2008-09-10 |
+------+---------+--------------+
1 row in set (0.00 sec)
上面的语句也可以写成:mysql> select * from student WHERE REGEXP_LIKE(name, '^.{4}$');
{4}重复的次数