mysql笔记三之条件、模糊、范围查询

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/kan2016 https://blog.csdn.net/kan2016/article/details/82771563

1.-- 条件查询 满足条件 就能够进入结果集
    -- 比较运算符
        -- >
        -- 查询大于18岁的信息
        
        -- <
        -- 查询小于18岁的信息

        -- >=
        -- <=
        -- 查询小于或者等于18岁的信息

        -- = 而不是 '=='
        -- 查询年龄为18岁的所有学生的名字
        select name from students where age = 18;
        -- != 或者 <>  实际开发中 最好使用 ! 表示不等于
        -- <> 表示不等于 是一种非常小众的用法
        select name from students where age <> 18;

    -- 逻辑运算符
        -- and
        -- 18岁以上的女性
        select *  from students where age > 18 and gender = 2;
        -- or
        -- 18以上或者身高超过180(包含)以上
        select * from students where not age > 18 or height >= 180;

        -- not  非
        -- 年龄不是18岁的学生
        select * from students where age != 18;
        select * from students where not age = 18;

        -- 年龄是小于或者等于18 并且是女性
        select * from students where age <= 18 and gender = "女";

select * from students where 1 > 0;

    2.-- 模糊查询
        -- like 
        -- % 表示任意字符可有可无
        -- 查询姓名中 以 "小" 开始的名字
        select * from students where name like "小%";
        -- 以 伦 结尾的学生
        select * from students where name like "%伦";

        -- 包含 杰
        select * from students where name like "%杰%"; 


        -- _ 表示任意一个字符
        -- 查询有2个字的名字
        select * from students where name like "__";

        -- 查询有3个字的名字
        select * from students where name like "___";

        -- rlike 正则
        -- 查询以 周开始的姓名
        -- match sub findall 这些只是python中封装的方法而已
        -- 正则表达式 r""
        select * from students where name rlike "^周.*";
        select * from students where name rlike "^周.*伦$";


        

    3.-- 范围查询
        -- in表示在一个非连续的范围内
        -- 查询 年龄为18、34的学生
        select * from students where age = 18 or age = 34;
        select * from students where age in (18,34);
        
        
        -- not in 不非连续的范围之内
        -- 年龄不是 18、34岁的学生的信息
        select * from students where age not in (18,34);

        -- 年龄不是 18、34岁之间的信息
        select * from students where age < 18 or age > 34;
        select * from students where not (age >= 18 and age <= 34);
        -- 18 ~ 34
        select * from students where age > 18 and age < 34;


        -- between ... and ...表示在一个连续的范围内  两边都会包含
        -- 查询 年龄在18到34之间的的信息
        select * from students where age between 18 and 34;
        
        -- not between ... and ...表示不在一个连续的范围内
        -- 查询 年龄不在在18到34之间的的信息
        select * from students where age not between 18 and 34;
        # 错误select * from students where age not (between 18 and 34);

    -- 空判断 null  不能够使用比较运算符
        -- 查询身高为空的信息
        # 错误 select * from students where height = null;
        -- 通过is 来判断是否为空
        select * from students where height is null;
        -- 查询身高不为空的学生
        select * from students where height is not null;

猜你喜欢

转载自blog.csdn.net/kan2016/article/details/82771563