标签:sel 山寨 学习 -- 模糊查询 谓词 学生 har ike
[<表名1>]<列名1> <比较运算符> [<表名2>]<列名2>
[<表名1>]<列名1> between [<表名2>]<列名2> and [<表名2>]<列名3>
连接运算符为=
查询每个学生以及选修课程的情况
mysql> select student.*, sc.* from student,sc where student.sno=sc.sno; +-------+--------+------+------+-------+-------+-----+-------+ | sno | sname | ssex | sage | sdept | sno | cno | grade | +-------+--------+------+------+-------+-------+-----+-------+ | 95001 | 李勇 | 男 | 20 | CS | 95001 | 1 | 92 | | 95001 | 李勇 | 男 | 20 | CS | 95001 | 2 | 85 | | 95001 | 李勇 | 男 | 20 | CS | 95001 | 3 | 88 | | 95002 | 刘晨 | 女 | 19 | IS | 95002 | 2 | 90 | | 95002 | 刘晨 | 女 | 19 | IS | 95002 | 3 | 80 | | 95004 | 张立 | 男 | 20 | IS | 95004 | 2 | 65 | | 95004 | 张立 | 男 | 20 | IS | 95004 | 3 | NULL | | 95004 | 张立 | 男 | 20 | IS | 95004 | 4 | NULL | | 95005 | 张三 | 男 | 23 | CS | 95005 | 2 | 84 | | 95005 | 张三 | 男 | 23 | CS | 95005 | 4 | NULL | | 96001 | 刘军 | 男 | 30 | IS | 96001 | 1 | 87 | | 96001 | 刘军 | 男 | 30 | IS | 96001 | 2 | 80 | | 96001 | 刘军 | 男 | 30 | IS | 96001 | 3 | 90 | | 96001 | 刘军 | 男 | 30 | IS | 96001 | 4 | 95 | | 96001 | 刘军 | 男 | 30 | IS | 96001 | 5 | NULL | | 96001 | 刘军 | 男 | 30 | IS | 96001 | 6 | NULL | | 96001 | 刘军 | 男 | 30 | IS | 96001 | 7 | 86 | | 97001 | 李四 | 男 | 26 | EN | 97001 | 4 | NULL | | 97001 | 李四 | 男 | 26 | EN | 97001 | 5 | NULL | +-------+--------+------+------+-------+-------+-----+-------+
select [ ] from <表名> order by <列名> [desc/asc];
mysql> select sno,sname from student order by sno; //默认为升序 +-------+--------+ | sno | sname | +-------+--------+ | 12001 | bgg | | 94001 | 山寨 | | 95001 | 李勇 | | 95002 | 刘晨 | | 95003 | 王敏 | | 95004 | 张立 | | 95005 | 张三 | | 96001 | 刘军 | | 96004 | 芙蓉 | | 97001 | 李四 | +-------+--------+ mysql> select sno,sname from student order by sno asc; //asc 修饰为升序 +-------+--------+ | sno | sname | +-------+--------+ | 12001 | bgg | | 94001 | 山寨 | | 95001 | 李勇 | | 95002 | 刘晨 | | 95003 | 王敏 | | 95004 | 张立 | | 95005 | 张三 | | 96001 | 刘军 | | 96004 | 芙蓉 | | 97001 | 李四 | +-------+--------+ 10 rows in set (0.05 sec) mysql> select sno,sname from student order by sno desc; //desc 修饰为降序 +-------+--------+ | sno | sname | +-------+--------+ | 97001 | 李四 | | 96004 | 芙蓉 | | 96001 | 刘军 | | 95005 | 张三 | | 95004 | 张立 | | 95003 | 王敏 | | 95002 | 刘晨 | | 95001 | 李勇 | | 94001 | 山寨 | | 12001 | bgg | +-------+--------+
mysql> select sno,sname from student order by sno desc,sname asc; //先按sno降序 再按sname升序
+-------+--------+
| sno | sname |
+-------+--------+
| 97001 | 李四 |
| 96004 | 芙蓉 |
| 96001 | 刘军 |
| 95005 | 张三 |
| 95004 | 张立 |
| 95003 | 王敏 |
| 95002 | 刘晨 |
| 95001 | 李勇 |
| 94001 | 山寨 |
| 12001 | bgg |
+-------+--------+
10 rows in set (0.05 sec)
select * from <表名> where <列名> between ‘a‘ and ‘b‘;
mysql> select * from student where sno between ‘94001‘ and ‘96001‘; +-------+--------+------+------+-------+ | sno | sname | ssex | sage | sdept | +-------+--------+------+------+-------+ | 94001 | 山寨 | 男 | 29 | CS | | 95001 | 李勇 | 男 | 20 | CS | | 95002 | 刘晨 | 女 | 19 | IS | | 95003 | 王敏 | 女 | 19 | MA | | 95004 | 张立 | 男 | 20 | IS | | 95005 | 张三 | 男 | 23 | CS | | 96001 | 刘军 | 男 | 30 | IS | +-------+--------+------+------+-------+
//not between and
mysql> select * from student where sno not between ‘94001‘ and ‘96001‘;
+-------+--------+------+------+-------+
| sno | sname | ssex | sage | sdept |
+-------+--------+------+------+-------+
| 12001 | bgg | M | 26 | CS |
| 96004 | 芙蓉 | 女 | 32 | CH |
| 97001 | 李四 | 男 | 26 | EN |
+-------+--------+------+------+-------+
% 替代一个或多个字符;
_ 仅替代一个字符;
[charlist] 字符列中的任何单一字符;
[!charlist] 或 [^charlist] 不在字符列中的任何单一字符;
//查询sno以95开头的学生信息 mysql> select * from student where sno like ‘95%‘; +-------+--------+------+------+-------+ | sno | sname | ssex | sage | sdept | +-------+--------+------+------+-------+ | 95001 | 李勇 | 男 | 20 | CS | | 95002 | 刘晨 | 女 | 19 | IS | | 95003 | 王敏 | 女 | 19 | MA | | 95004 | 张立 | 男 | 20 | IS | | 95005 | 张三 | 男 | 23 | CS | +-------+--------+------+------+-------+ 5 rows in set (0.05 sec) //查询sno以01结尾的学生信息 mysql> select * from student where sno like ‘%01‘; +-------+--------+------+------+-------+ | sno | sname | ssex | sage | sdept | +-------+--------+------+------+-------+ | 12001 | bgg | M | 26 | CS | | 94001 | 山寨 | 男 | 29 | CS | | 95001 | 李勇 | 男 | 20 | CS | | 96001 | 刘军 | 男 | 30 | IS | | 97001 | 李四 | 男 | 26 | EN | +-------+--------+------+------+-------+ 5 rows in set (0.05 sec)
2018-05-01 20:24:33
标签:sel 山寨 学习 -- 模糊查询 谓词 学生 har ike
原文地址:https://www.cnblogs.com/joy9707/p/8975408.html