查询语句

  1. SELECT 列名称 FROM 表名称;
  2. -- 查询单独的数据
  1. SELECT * FROM 表名称
  2. -- 查询所有的数据
  1. SELECT 列名称 FROM 表名称;
    1. 查询某列的数据
  2. SELECT * FROM 表名称
    1. 查询表中所有的数据 *:代表所有的列

1.使用比较运算符

  • =
  • >
  • <
  • =

  • <=
  • != <> 不等于
  • !< 不小于
  • !> 不大于

语法

  1. -- 查询大于90分的学生
  2. select * from score where score >= 90;
  1. 筛选的条件需要添加到我们where的后面

2.多个条件筛选

  • or
  • and
  • not
  • between
  • not between
  1. and :并且 需要同时满足这个条件


      1. -- 查询成绩大于90 并且 姓名是王五 and
      2. select * from score where score >= 90 and user_name = '王五';
  2. or:或者 满足其中的一个条件

    1. -- or 查询 姓名=张三 或者姓名=王五
    2. select * from score where user_name = '张三' or user_name = '王五';
  3. not: 不是

  4. between:范围查询

    1. -- betweem 范围查询 查询 成绩是90-95
    2. select * from score where score between 90 and 95;
  5. not between :不包含这个范围

    1. -- not between查询不在 90-95之间
    2. select * from score where score not between 90 and 95;

3.模糊查询

通配符

  • %:任意字符串
  • _:任意单个字符
  • []:范围内单个字符
  • [^]:范围外的单个字符
  1. %:任意字符

    1. -- % 匹配任意字符
    2. -- 获取姓名以王开头的
    3. select * from score where user_name like '王%';
    4. -- 获取中间五的姓名
    5. select * from score where user_name like '%五%';
  2. 单个字符

    1. -- 获取x三开的名字
    2. select * from score where user_name like '_三';

3.NULL值

  1. select * from score where gender is null; -- 查询为null
  2. select * from score where gender is not null; -- 查询不为null

4.获取前几条数据 top

  1. select top 2 * from score;-- 获取表中的前两条数据

5.排序(order by)

  • 升序 asc
  • 降序 desc
    1. select * from score order by score desc; -- 降序
    2. select * from score order by score asc; -- 升序