条件查询

  • 查询所有列

    select * from 表名;
    
    select * from student;
    
  • 查询指定列

    select 列名1 列名2 from 表名
    
    select stu_id, stu_name from student;
    
  • 使用as 作为表或者列的别名进行查询

    select 列名1 as 别名1,列名2 as 别名2 from 数据表 where.....
    
    select stu_id as 学号,stu_name as 姓名 from student
    

注意: 别名的顺序和写的顺序有关,谁写在前面谁就在前面。

  • 查询消除重复的结果

    select distinct 列名 from 表名 where .....
    
    select distinct gender from students;
    

因为性别只有那么几类,所以重复大量的出现意义并不大

条件查询的基本句式

  select * from 表名 where 条件;
  
  后面的条件可以是:
        
        1. 逻辑运算
        
        2. 比较运算

        3. 范围查询

        4. 模糊查询

        5. 空判断

逻辑运算:

  • and, or , not

    这里我们需要注意的就是 not 只对靠近的一个条件起作用
    
    例如:我们要查询年龄不在 18岁以上的女性 这个范围内的信息
    
    select * from students where not age>18 and gender='女'; 这样的很明显是错误的,例如35岁的女性显然是符合条件的,但是这样查询后就不符合条件了
    
    select * from students where not (id>18 and gender='女');
    

范围查询:

  • in, not in , between ... and ...

    select * from students where id in (1,2,3,5);
    
    select * from studnets where id between 3 and 8;
    
    select * from students where id not in (1,2,3,4);
    
  • 其中 between ... and ... 表示的是一个连续的

  • in(1,2,5,6) 只是在这几个中

模糊查询

  • like %表示任意字符,_表示一个字符

    查询姓黄的学生
    
    select * from students where name like '黄%';
    
    查询姓黄并且“名”是一个字的学生
    
    select * from students where name like '黄_';
    
    查询姓黄或叫靖的学生
    
    select * from students where name like '黄%' or name like '%靖';
    

判null查询

posted @ 2020-09-26 11:46  江湖混子  阅读(193)  评论(0编辑  收藏  举报