Day3 - 数据库SQL语句——条件查询

1、条件

  1. 使用where子句对表中的数据筛选,符号条件的数据会出现在结果集中
  2. 语法如下:
  3. select 字段1,字段2 from 表名 where 条件;
  4. 例:
  5. select * from students where id=1;
  6. where后面支持多种运算符,进行条件的处理
  7. 比较运算
  8. 逻辑运算
  9. 模糊查询
  10. 范围查询
  11. 空判断

2、比较运算符

  1. 等于: =
  2. 大于: >
  3. 大于等于: >=
  4. 小于: <
  5. 小于等于: <=
  6. 不等于: != <>
  7. 1:查询小乔的年龄
  8. select age from students where name=’小乔’
  9. 2:查询20岁以下的学生
  10. select * from students where age<20
  11. 3:查询家乡不在北京的学生
  12. select * from students where hometown!=’北京’
  13. 练习:
  14. 1、查询学号是’007’的学生的身份证号
  15. 2、查询’1班’以外的学生信息
  16. 3、查询年龄大于20的学生的姓名和性别
  17. 逻辑运算符
  18. and
  19. or
  20. not
  21. 1:查询年龄小于20的女同学
  22. select * from students where age<20 and sex=’女’
  23. 2:查询女学生或’1班’的学生
  24. select * from students where sex=’女’ or class=’1班’
  25. 3:查询非天津的学生
  26. select * from students where not hometown=’天津’
  27. 练习:
  28. 1、查询河南或河北的学生
  29. 2、查询’1班’的’上海’的学生
  30. 3、查询非20岁的学生

3、模糊查询

  1. like
  2. %表示任意多个任意字符
  3. _表示一个任意字符
  4. 1:查询姓孙的学生
  5. select * from students where name like ‘孙%’
  6. 2:查询姓孙且名字是一个字的学生
  7. select * from students where name like ‘孙_
  8. 3:查询叫乔的学生
  9. select * from students where name like ‘%乔’
  10. 4:查询姓名含白的学生
  11. select * from students where name like ‘%白%’
  12. 练习:
  13. 1、查询姓名为两个字的学生
  14. 2、查询姓百且年龄大于20的学生
  15. 3、查询学号以1结尾的学生

4、范围查询

  1. in表示在一个非连续的范围内
  2. 1:查询家乡是北京或上海或广东的学生
  3. select * from students where hometown in(‘北京’,’上海’,’广东’)
  4. between ... and ...表示在一个连续的范围内
  5. 2:查询年龄为1820的学生
  6. select * from students where age between 18 and 20
  7. 练习:
  8. 1、查询年龄在181922的女生
  9. 2、查询年龄在2025以外的学生

5、空判断

  1. 注意:null''是不同的
  2. 判空is null
  3. 1:查询没有填写身份证的学生
  4. select * from students where card is null
  5. 判非空is not null
  6. 2:查询填写了身份证的学生
  7. select * from students where card is not null

猜你喜欢

转载自www.cnblogs.com/hyf224317/p/11741945.html
今日推荐