SQL语句高级(八)——not like、year函数

一、not like模糊查询
查询student表中不姓C的同学记录:
mysql> select * from student where sname not like 'C%';
+-----+--------+---------------------+-------+-------+
| sno | sname  | sbirthday           | class | ssex  |
+-----+--------+---------------------+-------+-------+
| 100 | Java   | 1977-09-01 00:00:00 | 95033 | man   |
| 104 | Python | 1977-10-11 00:00:00 | 95033 | man   |
| 105 | JS     | 1974-11-11 00:00:00 | 95033 | woman |
+-----+--------+---------------------+-------+-------+
3 rows in set (0.09 sec)

二、查询student表中每个学生的姓名和年龄
年龄:当前年份减去出生年份
mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
|        2019 |
+-------------+
1 row in set (0.17 sec)

mysql> select year(sbirthday) from student;
+-----------------+
| year(sbirthday) |
+-----------------+
|            1977 |
|            1975 |
|            1976 |
|            1974 |
|            1977 |
|            1974 |
+-----------------+
6 rows in set (0.17 sec)

mysql> select sname,year(now())-year(sbirthday) as '年龄' from student
+--------+------+
| sname  | 年龄 |
+--------+------+
| Java   |   42 |
| C      |   44 |
| C++    |   43 |
| C#     |   45 |
| Python |   42 |
| JS     |   45 |
+--------+------+
6 rows in set (0.06 sec)

三、多字段排序
以班号和年龄从大到小的顺序查询student表中的全部记录:
mysql> select * from student 
	-> order by class desc,sbirthday;  --注意下sbirthday即可!!
+-----+--------+---------------------+-------+-------+
| sno | sname  | sbirthday           | class | ssex  |
+-----+--------+---------------------+-------+-------+
| 102 | C++    | 1976-02-11 00:00:00 | 95035 | woman |
| 103 | C#     | 1974-12-01 00:00:00 | 95034 | woman |
| 101 | C      | 1975-08-21 00:00:00 | 95034 | woman |
| 105 | JS     | 1974-11-11 00:00:00 | 95033 | woman |
| 100 | Java   | 1977-09-01 00:00:00 | 95033 | man   |
| 104 | Python | 1977-10-11 00:00:00 | 95033 | man   |
+-----+--------+---------------------+-------+-------+
6 rows in set (0.00 sec)

猜你喜欢

转载自blog.csdn.net/qq_37150711/article/details/87308522
今日推荐