一 、基本的查询语句
1.1 查询表中指定列
select <col1_name>,<col2_name>.... -- select 子句,列名非字符串因此不加双引号
from <tbname>; -- from 子句 #SQL 语句以;结束
例如:从学生表中查出两列
select 姓名, 性别 from student;
1.2 查询表中所有列
select * from <tb_name>; -- 星号 (*):查询出所有列
例如: 从学生表中查出所有列
select * from student;
1.3 使用 as 为列名设定别名
select 姓名 as name, 性别 as '人类性别’from student;
1.4 选取结果删除重复数据值
select distinct name from student; --选取表中name列,不包含重复值
select distinct * from student; -- 只有所有列都重复时,才算做重复值被删除
1.5 SQL简单查询之书写规则:
![87ed58b78a2fbeae5da66214c4416e72.png](https://img-blog.csdnimg.cn/img_convert/87ed58b78a2fbeae5da66214c4416e72.png)
1.6 练习 查询学生表
![5dc055c736578bcafe704275692b5763.png](https://img-blog.csdnimg.cn/img_convert/5dc055c736578bcafe704275692b5763.png)
二、 指定查询条件
2.1 where 限定查询条件
select 姓名,学号 from student where 姓名=‘猴子’; -- 使用where限定查询条件,只返回姓名为猴子的行
SQL 运行顺序 3 select 姓名,学号
1 from student
2 where 姓名=‘猴子’;
2.2 练习 注意SQL语句只认识英文符号
![e7f7fd2d2b393ec8adf0f09fbeed7ea4.png](https://img-blog.csdnimg.cn/img_convert/e7f7fd2d2b393ec8adf0f09fbeed7ea4.png)
三、注释和SQL语句注意事项
![e7a1e90e99896ae28b0bd5d63cacb6b3.png](https://img-blog.csdnimg.cn/img_convert/e7a1e90e99896ae28b0bd5d63cacb6b3.png)
![44e56bd834a261e77975bf23ffcfea36.png](https://img-blog.csdnimg.cn/img_convert/44e56bd834a261e77975bf23ffcfea36.png)
四、运算符
4.1 算数运算符
- 5+null=null 数值与NULL值运算后依然为NULL
![c11595093f4e6531e7947cd6ca99f26f.png](https://img-blog.csdnimg.cn/img_convert/c11595093f4e6531e7947cd6ca99f26f.png)
4.2 比较运算符
![96fdcee6a39d9a2654e4070c9df824b7.png](https://img-blog.csdnimg.cn/img_convert/96fdcee6a39d9a2654e4070c9df824b7.png)
4.3 逻辑运算符
![e9483059f5236f549ee23eb98e39ac8f.png](https://img-blog.csdnimg.cn/img_convert/e9483059f5236f549ee23eb98e39ac8f.png)
[1]
4.41 练习 算数运算符和比较运算符
![49fa8c8951d01ab7623b090369f3aafe.png](https://img-blog.csdnimg.cn/img_convert/49fa8c8951d01ab7623b090369f3aafe.png)
![4305092bf9d2a0d43719e140ef5f503a.png](https://img-blog.csdnimg.cn/img_convert/4305092bf9d2a0d43719e140ef5f503a.png)
4.42 练习 复杂的条件查询
![680e4e754281ded36db1e110c53c24e7.png](https://img-blog.csdnimg.cn/img_convert/680e4e754281ded36db1e110c53c24e7.png)
五、字符串的模糊查询
5.1 like
% 表示任意字符串
select * from student where 姓名 like '猴%'; -- 查询姓猴的
select * from student where 姓名 like '%猴'; -- 查询以猴结尾的
select * from student where 姓名 like '%猴%'; -- 查询带猴字的名字
_ 下划线表示任意1个字符
select * from student where 姓名 like '王_'; -- 查询姓王的两个字名字
select * from student where 姓名 like '王__'; -- 两个下划线 查询姓王的三个字名字
5.2 练习 模糊查询
![b9c633032c3074165e56b4ae397721b1.png](https://img-blog.csdnimg.cn/img_convert/b9c633032c3074165e56b4ae397721b1.png)
六、SQLzoo练习
练习后补充笔记
where expr 1 XOR expr2 满足条件一或者条件二,但排除两者都满足者
left(‘string’,1) 取字符串左起第一个位置
round(92500,-2) 约到百位 round(92500,-3)约到千位
![3348d9763472461667c4e5927a222722.png](https://img-blog.csdnimg.cn/img_convert/3348d9763472461667c4e5927a222722.png)
![145c8abdd1b202eed03d62bad4ef088a.png](https://img-blog.csdnimg.cn/img_convert/145c8abdd1b202eed03d62bad4ef088a.png)
![44357081e0dccf5e332b4e7f0760120f.png](https://img-blog.csdnimg.cn/img_convert/44357081e0dccf5e332b4e7f0760120f.png)
![694e62146efea0028be3b0642d372861.png](https://img-blog.csdnimg.cn/img_convert/694e62146efea0028be3b0642d372861.png)
![aeb71e501e67275ebe09175eea2b5736.png](https://img-blog.csdnimg.cn/img_convert/aeb71e501e67275ebe09175eea2b5736.png)
![86a97c4cbb0ac131ac23c454a7de8293.png](https://img-blog.csdnimg.cn/img_convert/86a97c4cbb0ac131ac23c454a7de8293.png)
![c169107ce9dddd9977999d83ece57f29.png](https://img-blog.csdnimg.cn/img_convert/c169107ce9dddd9977999d83ece57f29.png)
![19591ddcefed18b2b5944d20bdb3cb7e.png](https://img-blog.csdnimg.cn/img_convert/19591ddcefed18b2b5944d20bdb3cb7e.png)
![9850490aae9ff8182cb7f076afefa820.png](https://img-blog.csdnimg.cn/img_convert/9850490aae9ff8182cb7f076afefa820.png)
参考
- ^该处图片来自https://wiki.jikexueyuan.com/project/sql/operators.html https://wiki.jikexueyuan.com/project/sql/operators.html