7)where子句

发布时间 2023-05-26 18:25:51作者: QianFa01

where子句:根据条件表达式从数据源中筛选符合条件的记录

select 字段列表
    from 数据源
        where 条件表达式;

1、比较运算符:

>    <    >=    <=    =    !=(<>)

注意等于是单个等于号,不等于也可以用<>表示;

例如,查找及格的项:

select * from choose where score>=60;

 例如:查找 张三 的考试成绩信息,需要用到内连接;

select s.student_no 学号,s.student_name 名字, c.score 成绩
    from student s join choose c on s.student_no=c.student_no
    where s.student_name = '张三';

 2、where实现内连接:

from 这儿使用逗号代替join;

select 字段列表
    from 表1,表2 where 关联条件;
select student_no 学号,student_name 名字,c.class_no 班级名
    from student s, classes c
    where s.class_no=c.class_no;

 3、is null判断是否为空

注意这儿的判断是否为空,不能用 =null 或者 != null ,这个表示  此条件永远为 null,通俗讲就是永远为false;

 例如 列出没有班级的学生:也是需要先用大外连接

select c.class_no 班号,class_name 班级名,department_name 院名,s.*
    from classes c left join student s
    on c.class_no=s.class_no;

 可以看到测控班没有学生,那么使用where子句进行筛选;

select c.class_no 班号,class_name 班级名,department_name 院名,s.*
    from classes c left join student s
    on c.class_no=s.class_no
    where s.student_no is null;

 4、between...and:

格式:表达式 between 值1 and 值2

select * from choose where score between 70 and 90;

select * from choose where choose_time between '2023-05-25 21:34:11' and '2023-05-25 21:34:41';

 5、in:

格式:表达式 in (值1,值2,...)

select ch.student_no 学号,c.course_name 课程名,ch.score 成绩
    from course c join choose ch on c.course_no=ch.course_no
    where c.course_name in ('c++','MySQL');

 6、like:

格式:表达式 like '模式'

通配符:% :匹配任意长度的任意字符;   _ :匹配一位任意字符;

select student_no 学号, student_name 姓名 from student
    where student_name like '张%';

注意:当碰到要查找的字符本身含有_时,查找时,会认为这个是通配符;

例如,查找user_为开头的,,若查找时格式为 ’user_%‘,并不是以这个为开头,而是‘user’开头的,会把_当作通配符处理;

select table_schema,table_type,table_name from information_schema.tables
    where table_name like 'user_%';

可以看到第一项为users,并不是user_开头的;

处理方法:

1)此时需要我们将其转义为下划线, 即使用转义字符 '\',所以格式为:这种方法是关系型数据库MySQL独有的,不适合于其他关联性数据库;

select table_schema,table_type,table_name from information_schema.tables
    where table_name like 'user\_%';

 可以看到正如我们想要的一样;

 2)我们也可以自定义通配符;适合于其他关联性数据库;

select table_schema,table_type,table_name from information_schema.tables
    where table_name like 'user#_%' escape '#';

一样的效果;

 7、and运算符

1)代替between  and;

select * from choose where score>=60 and score<=90;

 2)where内连接,查找信息

select s.student_no 学号,s.student_name 姓名,c.score 成绩
    from student s,choose c
    where s.student_no=c.student_no
    and s.student_name='张三';

 3)三表内连接;

格式:

select 字段列表
    from 表1,表2,表3
    where 关联条件1 and 关联条件2;

例如 检索所有学生信息:

select s.student_name 姓名,s.student_no 学号,c.course_name 课程,ch.score 成绩
    from student s,course c,choose ch
    where s.student_no=ch.student_no and c.course_no=ch.course_no; 

 8、or:

select ch.student_no, c.course_name,ch.score
    from course c,choose ch
    where c.course_no=ch.course_no
    and (c.course_name='c++' or c.course_name='MySQL');

 9、not:

格式:not 逻辑表达式 或者 !(逻辑表达式)

select * from course where up_limit!=60;
select * from course where !(up_limit=60);
select * from course where not(up_limit=60);

 10、运算符取反:

1):is null 取反;将有学生的班级列出;

select distinct c.*
    from student s right join classes c
    on c.class_no=s.class_no
    where s.student_no is not null;

 2)in 取反,not in,要注意为null的情况

select * from student where class_no in(1,2);
select * from student where class_no not in(1,2);

 但是当in中有null的时候:

select * from student where class_no in(1,2,null);
select * from student where class_no not in(1,2,null);

 可以看到当in有null时,对它取反,得到 空结果集;

产生原因

上述 is null 判断为空时说过,=null 和 != null 的情况,其结果都是false;必须要用is null 判断空;

class_no in(1,2,null)       ==> class_no == 1 or class_no == 2 or class_no == null; 此处第三个条件 class_no == nul 条件永远为false;
                            ==> class_no == 1 or class_no == 2
                            
class_no not in(1,2,null)   ==> class_no != 1 and class_no != 2 and class_no != null ;此处第三个条件 class_no != nul 条件永远为false;那么该条件与后最终就是false;
                            ==> null

因此,当对有null值的in取反时,其结果集就是空的;