![556c322df687b683cedf169cc8d1afff.png](https://i-blog.csdnimg.cn/blog_migrate/4a29787739ad5e58b1d74bad04d72a71.jpeg)
- 单行注释:-- 注释的内容
- 多行注释: /* 注释的内容 */
- 本文参考的数据表如下
![1b07f4cbf1332975347d73df599c7648.png](https://i-blog.csdnimg.cn/blog_migrate/99f5ea94b421893236aed20c566782be.jpeg)
一. 基本查询语句
![c94a6210c865809373925ada58b0c0a6.png](https://i-blog.csdnimg.cn/blog_migrate/4d1de15544f283acc75feb89d2296c11.png)
1.1 查询数据列
- select <列名1>,<列名2> from<表名>;
- 从student学生数据表中查询"姓名列"和"性别列"的数据
【select 姓名,性别 from student;】
1.2 查询全部
- select * from<表名>;
- 从student学生数据表中查询"所有"列的数据
【select * from student;】
1.3 列名命名别名
- select <列名1> as <别名A>,<列名2> as <别名B> from <表名>;
- 对选中的数据列命名别名,若别名为中文情况下要用英文单引号括起来(原始表的列名为英文转化别名为中文可以更好理解)
【select 姓名 as s_name,性别 as '人类性别' from student;】
1.4 删除重复查询结果
- select distinct <列名1>,<列名2> from <表名>;
- 查询选中的student数据表"学号列"和"姓名列"存在的重复值并在查询结果中删除(保留一个),选中多列则多列数值都同时重复
【select distinct 学号,姓名 from student】
二. 指定查找条件
![7fc18b4cbbdebf25454ba2aa2b0e59a8.png](https://i-blog.csdnimg.cn/blog_migrate/983ff4bf322d68a9125acadfbe2cc529.png)
2.1 查找条件
- select <列名1>,<列名2> from<表名> where <列名1>='指定条件';
- where语句:查询student数据表中姓名为李彦宏的'姓名列''学号列'
【select 姓名,学号 from student where 姓名='李彦宏';】
2.2 SQL语句运行顺序
- 先运行 from <表名>
- 在运行 where <列名1>='指定条件'
- 最后运行选择要查询的数据列 select <列名1>,<列名2>
三. 运算符
3.1 特别注意
- 所有含有空值的运算结果还是空值,例如【5+Null=Null】
- 【'10'<'2'】两个数字存储的类型是字符串类型,字符串比较规则根据字典顺序进行排序的
![0794c26f477135cb74c068ca7aef7c84.png](https://i-blog.csdnimg.cn/blog_migrate/61dacd18b89b232b7049a070ecc8872c.png)
算术运算符 & 比较运算符
![e97c9775b9b22bb30ce02b97954061bc.png](https://i-blog.csdnimg.cn/blog_migrate/79263db30185c9b5b0525e72b812cde0.jpeg)
3.2 算数运算符 /
- select <列名1>,<列名2>,<算数运算列> as <别名> from <表名>
- where语句:查询student数据表中姓名为猴子的'姓名列''学号列'
【select 学号,成绩,成绩/100 as '百分比成绩' from score;】
3.3 比较运算符 <
- select <列名1>,<列名2> from <表名> where <列名1> <比较运算条件>
- 查询score数据表中学号列,成绩列中成绩小于60的数据
【select 学号,成绩 from score where 成绩<60;】
3.3 比较运算符 < 日期型
- select <列名1>,<列名2> from <表名> where <列名1> <比较运算条件>
- 查询student数据表中姓名列,出生日期列中出生日期小于1990-01-01的数据,1990-01-01是日期型数据要英文引号阔上
【select 姓名,出生日期 from student where 出生日期<'1990-01-01';】
逻辑运算符
![b6981c96133e343dd8a87237c0f53087.png](https://i-blog.csdnimg.cn/blog_migrate/3bb0690d59b0bcfff665b49319c9b7c7.jpeg)
3.4 not逻辑运算符
- select <列名1>,<列名2> from <表名> where not <列名1> <比较运算条件>
- 查询score数据表中学号列,成绩列中成绩不大于60的数据
【select 学号,成绩 from score where not 成绩>=60;】
- select <列名1>,<列名2> from <表名> where <列名1> <比较运算条件> and <列名2> <比较运算条件>
- 查询score数据表中学号列,成绩列中成绩大于等于60并且小于等于90的数据
【select 学号,成绩 from score where 成绩>=60 and 成绩<=90;】
3.5 and逻辑运算符
- select <列名1>,<列名2> from <表名> where <列名1> <比较运算条件> and (<列名1> <比较运算条件> or <列名2> <比较运算条件>)
- 查询student数据表中姓名列,性别列中性别等于男并且姓名为李彦宏或者姓名为马云的数据(括号内的运算符优先于括号外的运算)
【select 姓名,性别 from student where 性别='男' and (姓名='李彦宏' or 姓名='马云');】
3.6 between逻辑运算符
- select <列名1>,<列名2> from <表名> where <列名> between <指定条件1> and <指定条件2>
- 查询score数据表中学号列,成绩列中成绩在60和90之间的数据
【select 学号,成绩 from score where 成绩 between 60 and 90;】
- 等同于下边的SQL语句,(是and运算符的简单写法注意between运算符会查找包含查找边界的值60 90)
【select 学号,成绩 from score where 成绩>=60 and 成绩<=90;】
3.7 or逻辑运算符
- select <列名1>,<列名2> from <表名> where <列名> <运算条件1> or <列名> <运算条件1>
- 查询score数据表中学号列,成绩列中成绩小于60或者大于90的数据
【select 学号,成绩 from score where 成绩<60 or 成绩>90;】
3.8 in逻辑运算符
- select <列名1>,<列名2> from <表名> where in (指定条件1,指定条件2)
- 查询student数据表中姓名列,性别列中姓名是李彦宏或者马云的数据
【select 姓名,性别 from student where 姓名 in ('李彦宏','马云');】
- 下边的为否定语句加not in运算符是or运算符的简便写法
【select 姓名,性别 from student where 姓名 not in ('李彦宏','马云');】
3.9 查询空值null
![4c39ee9cfa13df19e8658f945fbb1398.png](https://i-blog.csdnimg.cn/blog_migrate/cc07328833b61d8f3672fdf5ef8a6b3a.png)
- select <列名1>,<列名2> from <表名> where <列名> is null
- 查询teacher数据表中教师号列,教师姓名列中教师姓名是空值null的数据
【select 教师号,教师姓名 from teacher where 教师姓名 is null】
- -- 下边的为否定语句加not (不是空值的数据)
【select 教师号,教师姓名 from teacher where 教师姓名 is not null】
四. 字符串查询
4.1 like字符串模糊查询(%号:表示查找任意的字符串)
- select <列名1> from<表名> where like 字符串条件;
- 查询student数据表中字符串包含李的数据
【select * from student where 姓名 like '李%';】
【select * from student where 姓名 like '%彦%';】
【select * from student where 姓名 like '%宏';】
4.2 like字符串模糊查询(_1个下划线表示一个字符)
- select <列名1> from<表名> where like 字符串条件;
- 查询student数据表中字符串为王开头的3个字符的数据(王__)这里是两个下划线
【select * from student where 姓名 like '王__';】
【select * from student where 姓名 like '_彦_';】
五. 基础练习
5.1 SQL语句练习题网址:https://sqlzoo.net/wiki/SQL_Tutorial
选择中文后,可选择对应的练习题进行练习
![92328a8982c1c7de6cb7d31b68643f7b.png](https://i-blog.csdnimg.cn/blog_migrate/783c84c820f99516001d009bcd0414de.jpeg)