- 主要内容:
一. 基本查询语句
select <列名1>, <列名2>, ……
from <表名>;
例:
select 姓名,性别
from student;
查询全部列例子:
select *
from student;
2. 为列设置别名
select 姓名 as s_name, 性别 as 人类性别
from student;
3. 删除重复数据
select distinct 姓名
from student;
distinct后有两列必须两列中信息都相同才会删除
select distinct 学号, 姓名
from student;
4. SQL 书写规则
1) SQL语句以英文分号结尾
2)SQL语句不区分关键字的大小写
3)列名不能加单引号,列名命名时不能有空格
4)符号只能使用英文符号
二. 如何指定查询条件
select 姓名,学号
from student
where 姓名= ‘猴子';
SQL 运行数据: 1. from --- 2. where --- 3. select
三. 注释和sql语句注意事项
1.单行注释:
-- 查询出全部列
select *
from student;
多行注释:
/*查找姓名是
猴子学生的学号*/
select 姓名, 学号
from student
where 姓名= '猴子';
2. sql语句注意事项
四. 运算符
- 算数运算符:
![fd5c5c48bc9213ad6b82b6790474a82d.png](https://img-blog.csdnimg.cn/img_convert/fd5c5c48bc9213ad6b82b6790474a82d.png)
2. 比较运算符:
![d403643c44980cc75a041d2c2d5c5602.png](https://img-blog.csdnimg.cn/img_convert/d403643c44980cc75a041d2c2d5c5602.png)
如何查询出null值?
![7d9f6ac7456842eb0eb8c9213b68ae39.png](https://img-blog.csdnimg.cn/img_convert/7d9f6ac7456842eb0eb8c9213b68ae39.png)
3. 逻辑运算符:
![92e0c7240fd1254e8b0b2494af97baca.png](https://img-blog.csdnimg.cn/img_convert/92e0c7240fd1254e8b0b2494af97baca.png)
五. 字符串模糊查询: like
![f99a90cf1fc8e478883291c9349d583d.png](https://img-blog.csdnimg.cn/img_convert/f99a90cf1fc8e478883291c9349d583d.png)
![a16d1aaf000e11ab10d9f6bd042701c1.png](https://img-blog.csdnimg.cn/img_convert/a16d1aaf000e11ab10d9f6bd042701c1.png)
- 练习:
- 基本语句
![ca820f238065c2547ecf5f355c7473de.png](https://img-blog.csdnimg.cn/img_convert/ca820f238065c2547ecf5f355c7473de.png)
![812944f222bbb3259bc00d4f9346f810.png](https://img-blog.csdnimg.cn/img_convert/812944f222bbb3259bc00d4f9346f810.png)
![9db1c0161d5dbe10c1f8eb57c48ee035.png](https://img-blog.csdnimg.cn/img_convert/9db1c0161d5dbe10c1f8eb57c48ee035.png)
![3d21127adb80f9b3bac597aab75e354d.png](https://img-blog.csdnimg.cn/img_convert/3d21127adb80f9b3bac597aab75e354d.png)
2. 指定条件
![e5047e1d3082d761b646363a5df824cf.png](https://img-blog.csdnimg.cn/img_convert/e5047e1d3082d761b646363a5df824cf.png)
4. 运算符
算数运算符:
![d8665d15238e5ce59b48af4f0d716c69.png](https://img-blog.csdnimg.cn/img_convert/d8665d15238e5ce59b48af4f0d716c69.png)
比较运算符:
![2c5050deffbd6591e96f960e264e425e.png](https://img-blog.csdnimg.cn/img_convert/2c5050deffbd6591e96f960e264e425e.png)
![79aeb04c99e4f37b0f785cf4b87a9c52.png](https://img-blog.csdnimg.cn/img_convert/79aeb04c99e4f37b0f785cf4b87a9c52.png)
查询null 值:
![03ba86af63c4f5fc7582a2f8417de81e.png](https://img-blog.csdnimg.cn/img_convert/03ba86af63c4f5fc7582a2f8417de81e.png)
![8863d6261092547a8b10824f70f19a13.png](https://img-blog.csdnimg.cn/img_convert/8863d6261092547a8b10824f70f19a13.png)
逻辑运算符:
![f4b65149f9ace13ab2b27073ce046e72.png](https://img-blog.csdnimg.cn/img_convert/f4b65149f9ace13ab2b27073ce046e72.png)
![57d70d2a6c04e63354c33b722aa5d39b.png](https://img-blog.csdnimg.cn/img_convert/57d70d2a6c04e63354c33b722aa5d39b.png)
![c07533b874223aa375b4041c80f96b53.png](https://img-blog.csdnimg.cn/img_convert/c07533b874223aa375b4041c80f96b53.png)
![2e6a01ce0095b41860c1b4d9bd947410.png](https://img-blog.csdnimg.cn/img_convert/2e6a01ce0095b41860c1b4d9bd947410.png)
5. 字符串模糊查询
姓猴:
![e7d70bf168b2a5c28651b39936a17301.png](https://img-blog.csdnimg.cn/img_convert/e7d70bf168b2a5c28651b39936a17301.png)
最后一个字是云:
![82c5af80c082671783e1fe268585a8ac.png](https://img-blog.csdnimg.cn/img_convert/82c5af80c082671783e1fe268585a8ac.png)
名字中带有猴:
![c234eafb58e741a2ccdd04626e9f9405.png](https://img-blog.csdnimg.cn/img_convert/c234eafb58e741a2ccdd04626e9f9405.png)
- sqlzoo
![f738770189f5e6fe3832229596f215c6.png](https://img-blog.csdnimg.cn/img_convert/f738770189f5e6fe3832229596f215c6.png)