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)
6.计算下个月过生日的人,比如这里我们再添加两条数据
# cat /angel/student.txt
3,wangwu,1919-05-01
4,zhaoliu,2018-08-10
现在的日期时间:
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2020-04-03 21:28:12 |
+---------------------+
1 row in set (0.00 sec)
计算下个月过生日的人:
mysql> select * from student WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH));
+------+--------+------------+
| id | name | birth |
+------+--------+------------+
| 3 | wangwu | 1919-05-01 |
+------+--------+------------+
1 row in set (0.00 sec)
7.NULL值的处理
在MySQL中 NULL表示“缺少未知值(a missing unknown value)”,并且与其他值的处理方式有所不同。
对于 NULL 的操作,务必 使用IS NULL和IS NOT NULL运算符。
注意:绝对不能使用算术比较运算符(>或<或<>)来操作 NULL。
在MySQL中,0或NULL表示false,其他表示true。布尔运算的默认真值是1
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)
这里用的是4个下划线_
5).正则表达式
MySQL提供的另一种模式匹配使用扩展的正则表达式。在使用此类型的模式是否匹配时,请使用REGEXP_LIKE()函数(或REGEXP或RLIKE运算符,它们是REGEXP_LIKE()的同义词)。
1) .匹配任何单个字符。
2) 匹配a,b或c。
3) 匹配任何字母,而匹配任何数字。
4) *匹配零个或多个在其之前的事物的实例。例如,x *匹配任意数量的x个字符, *匹配任意数量的数字,而.*匹配任意数量的任何字符。
如果正则表达式模式匹配成功,则该模式匹配成功。(这与LIKE模式匹配不同,LIKE模式匹配仅在模式匹配整个值时才成功。)要定位模式以使其必须与被测试值的开头或结尾相匹配,请在开头使用^或在结尾使用$。
例如:
为了演示,我们的数据如下:
mysql> select * from student;
+---+------------------+--------------+
| id | name | birth |
+---+------------------+--------------+
|1 | zhangsan | 1997-01-01 |
|2 | lisi | 2008-09-10 |
|3 | wangwu | 1919-05-01 |
|4 | zhaoliu | 2018-08-10 |
|5 | ZHOUXINGXING | 1956-08-25 |
|6 | DASHENG | 1919-12-23 |
+------+----------------+-------------+
6 rows 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}重复的次数
页:
[1]