语法

  1. select ...
  2. from ...
  3. where ...
  4. order by ...
  5. limit ...

select

别名

  1. SELECT
  2. `name` 名字
  3. FROM
  4. employee

image.png

*:所有

  1. SELECT
  2. *
  3. FROM
  4. employee

image.png

case:判断语句相当于if

  1. SELECT
  2. `name` 名字,
  3. CASE
  4. ismale
  5. WHEN 1 THEN
  6. '男' ELSE '女'
  7. END 性别
  8. FROM
  9. employee

image.png

  1. SELECT id, `name`,
  2. case
  3. when ismale = 1 then '男'
  4. else '女'
  5. end sex,
  6. case
  7. when salary>=10000 then '高'
  8. when salary>=5000 then '中'
  9. else '低'
  10. end `level`,
  11. salary
  12. FROM employee;

distinct:去重

  1. SELECT DISTINCT
  2. e.location 居住地址
  3. FROM
  4. employee AS e

image.png

from

where

= 相当于js中 ==

  1. SELECT
  2. *
  3. FROM
  4. employee AS e
  5. WHERE
  6. e.`name` = '张云'

in:在什么之中

  1. SELECT
  2. *
  3. FROM
  4. department AS d
  5. WHERE
  6. -- 在公司id 1 在公司id 3 的部门
  7. d.companyId IN(1,3)

image.png

is:为null值

  1. SELECT
  2. *
  3. FROM
  4. employee
  5. -- 查询那些员工地址为null 空值 null 不相等
  6. WHERE location IS NULL

image.png

is not:不是null值

  1. SELECT
  2. *
  3. FROM
  4. employee
  5. -- 查询那些员工地址为null 空值 null 不相等
  6. WHERE location IS NOT NULL

image.png

< > <= >= :小于 大于 小于或等于 大于或等于

between:在什么之间

  1. SELECT
  2. *
  3. FROM
  4. department AS d
  5. WHERE
  6. -- 在公司id 1 公司id3 之间的部门
  7. d.companyId BETWEEN 1 AND 3

image.png

like:模糊搜索

% 和_

  1. SELECT
  2. *
  3. FROM
  4. employee
  5. -- %任意字符
  6. -- 匹配袁前面或后面任意字符
  7. WHERE `name` LIKE '%袁%'

image.png

  1. SELECT
  2. *
  3. FROM
  4. employee
  5. -- '_'一个字符
  6. -- 匹配性袁且名为一个字
  7. WHERE `name` LIKE '袁_'

image.png

and:并且 相当于 &&

and 优先级比or高

or:或者

  1. SELECT * from employee
  2. WHERE `name` like '张%' and (ismale=0 and salary>=12000
  3. or
  4. birthday>='1996-1-1');

order by:排序

asc 升序

  1. SELECT *
  2. FROM employee
  3. -- 升序排列
  4. ORDER BY birthday ASC

image.png

desc 降序

  1. SELECT *
  2. FROM employee
  3. -- 降序排列
  4. ORDER BY birthday DESC

image.png

limit:n,m跳过n条数据,取出m条数据

  1. SELECT *
  2. FROM employee
  3. -- 降序排列
  4. ORDER BY birthday DESC
  5. -- 分页
  6. LIMIT 0, 5

image.png

运行顺序

  • 先从from开始
  • where
  • select
  • order by
  • limit
  • select

    练习

    ```sql — 查询user表,得到账号为admin,密码为123456的用户 — 登录

SELECT * from user WHERE loginid = ‘admin’ and loginpwd = ‘123123’;

  1. ```sql
  2. -- 查询员工表,按照员工的入职时间降序排序,并且使用分页查询
  3. -- 查询第3页,每页5条数据
  4. -- limit (page-1)*pagesize, pagesize
  5. SELECT * FROM employee
  6. ORDER BY employee.joinDate desc
  7. LIMIT 10,5
  1. -- 查询工资最高的女员工
  2. SELECT * FROM employee
  3. WHERE ismale = 0
  4. ORDER BY salary desc
  5. limit 0,1;
  1. -- 查询女性2000年以后入职或出生日期在1998年以后的工资情况
  2. SELECT
  3. `name`,
  4. location,
  5. joinDate,
  6. birthday,
  7. ismale,
  8. CASE
  9. WHEN salary >= '8000' THEN
  10. '高工资'
  11. WHEN salary >= '5000' THEN
  12. '中等工资' ELSE '低工资'
  13. END salary
  14. FROM
  15. employee
  16. WHERE ismale = 0 AND (joinDate > '2000-1-1' OR birthday>'1998-1-1')

资料

companydb.sqlstaff.sql