一、记录要点

1.select <列名><列名> from <表名>

2.*可以代表所有列

3.用as 设置别名,如select 姓名 as s_name, 姓名 as '人类性别' from student

4.distinct(用来删除一列中的重复数据),单列之前,是select distinct 姓名 from student,多列之前,组合相同才删除

重点注意:列名不能加单引号,有特殊含义,不是字符串不能加单引号,列名中间不能加空格!

5.用where语句指定查询条件(就是放在最后),譬如select 姓名,学号 from student where 姓名 = '猴子'(区分python中的=是赋值,==才是是否相等)

重点注意:第一步运行from student(哪张表) where 姓名 = '猴子'(按照书写顺序运行),第二步运行select语句

6.单行注释--(加空格),多行注释/*查找姓名是猴子学生的学号*/

7.算术运算符(+-*/)

比较运算符(= <>(不等于) >大于,>=大于等于,<小于,<=小于等于)

字符串类型‘10’<'2',排序的时候如果10在2前面,需要判断是不是字符串

8.Not 否定一个条件:直接加在where后面的

and 并且:在实际使用中往往同时使用多个查询条件来选择出来

between 范围查询

or 或者

in or的简单写法(有点意思)

9.模糊查询,like,%代表查询任意的字符串

10.一个下划线(_)代表任意一个字符

二、SQL实操记录

记录1:



-- 查询姓名,性别
select 姓名,性别 from student;
-- 查询全部列
select *from student;
-- 将列名设置别名,中间需要用,隔开!
select 姓名 as s_name,性别 as '人类性别' from student;
-- 消除重复值
select distinct 姓名 from student;
-- 都一样才消除
select distinct 学号,姓名 from student;
/*选择姓名是猴子学生的学号*/
select 姓名,学号 from student where 姓名 = '猴子';
-- 将成绩/100作为'百分比成绩'
select 学号,成绩,成绩/100 as '百分比成绩' from course;
-- 选择出成绩不及格的学生
select 学号,成绩 from course where 成绩 < 60;
-- 选择出出生日期小于'1990-01-01'
select 姓名,出生日期 from student where 出生日期<'1990-01-01';
-- 选择是空值的结果
select 教师号,教师姓名 from teacher where 教师姓名 is null;



输出结果1:




distinct 加索引 select distinct as_字符串


记录2:


-- 选择不是空值的结果
select 教师号,教师姓名 from teacher where 教师姓名 is not null;
-- 选择成绩大于等于60(成绩>=60),一般不使用Not编写sql语句
select 学号 from course where not 成绩 >= 60;
-- 用and语句查询成绩在60-90这个区间的学生
select 学号 from course where 成绩>=60 and 成绩<=90;
select 学号 from course where 60<=成绩<=90;
select 学号 from course where 成绩 between 60 and 90;
-- 查询性别是男并且姓名是猴子或者马云的学生
select 学号,姓名 from student where 性别 = '男' and 姓名 = ('马云'or '猴子');
select 学号,姓名 from student where 性别 = '男' and 姓名 in ('马云','猴子');
-- or的用法
select 学号,成绩 from course where 成绩< 60 or 成绩>90;
-- 模糊查询
select * from student where 姓名 like '%猴';
select * from student where 姓名 like '猴%';


输出结果2:


distinct 加索引 select distinct as_distinct 加索引_02


记录3:


select * from student where 姓名 like '%猴%';
select * from student where 姓名 like '马_';
select * from student where 姓名 like '猴%';
select * from student where 姓名 like '%猴';
select * from student where 姓名 like '%猴%';


输出结果3:


distinct 加索引 select distinct as_字符串_03


三.SQLZOO题目操作


distinct 加索引 select distinct as_单引号_04


distinct 加索引 select distinct as_字符串_05


distinct 加索引 select distinct as_select distinct_06


distinct 加索引 select distinct as_设置别名_07


distinct 加索引 select distinct as_select distinct_08


distinct 加索引 select distinct as_distinct 加索引_09


distinct 加索引 select distinct as_设置别名_10


distinct 加索引 select distinct as_字符串_11


distinct 加索引 select distinct as_设置别名_12


distinct 加索引 select distinct as_distinct 加索引_13


distinct 加索引 select distinct as_distinct 加索引_14


distinct 加索引 select distinct as_select distinct_15