基础查询 — 投影、选择

准备
  • 数据查询操作均采用的是MySQL
  • 示例数据库采用的是northwind 示例数据库。
  • 数据库导入手册
关于northwind 示例数据库
  • 查询数据库中的表

    show tables;
    

    在这里插入图片描述

  • 查询表的表属性

    desc xxx(表名);
    

在这里插入图片描述

投影操作
1.语法
  • 投影运算:选择表中的全部或者部分列。

  • 语法

    select 字段列表
    from   表名;
    
2.特点
  • 查询列表可以是:表中的字段、常量值、表达式、函数。
  • 查询的结果是一个虚拟的表格。
3.实例
  • 查询常量值:查询helow world

    select 'hello world';
    
  • 查询表中单个、多个字段。

    # 在 employees 表中,查询 first_name 字段
    select first_name from employees;
    
    # 在 employees 表中,查询 first_name,last_name 和 salary 字段
    select first_name, last_name, salary
    from employees;
    
  • 查询表中所有字段:用 * 代替所有字段。

    select * from northwind.employees;
    
  • 使用as关键字起别名:将选择出的数据赋予新的列名。

    # 在 employees 表中,查询 employee_id, last_name 和 annual_salary 字段
    # 将月薪乘12作为年薪出现
    
    select employee_id, last_name, salary * 12 as annual_salary
    from employees;
    
  • 去重:使用distinct函数去重。

    # 查询 employees 表中的不同的 job_id
    select distinct job_id
    from northwind.employees;
    
  • 拼接操作:使用concat函数进行拼接。

    # 查询 employees 表的 first_name 和 last_name,拼接为 fullname,显示结果为 employee fullname
    
    select concat(first_name, ' ', last_name) as "employee fullname"
    from employees;
    
选择(筛选)操作
1.语法
  • 通过不同的筛选条件来选择到相应的字段

  • 语法结构

    select 字段列表
    from   表名
    where  筛选条件;
    
2.按条件表达式筛选
  • 简单条件运算:> < = != <> >= <=

  • 查询employees 表中,工资大于 12000 的员工信息

    select * 
    from employees
    where salary > 12000
    
  • 查询 employees 表中,部门编号不等于 90 的员工 last_name 和部门编号

    select last_name, department_id 
    from employees
    where department_id <> 90;
    或者
    select last_name, department_id 
    from employees
    where department_id != 90;
    
3.按逻辑表达式执行
  • 常用逻辑运算符:&& || ! and or not

  • 查询 employees 表中,工资在 10000 到 20000 之间的员工 last_name和工资

    select last_name, salary
    from employees
    where salary >= 10000 and salary <= 20000;
    或者
    select last_name, salary
    from employees
    where salary >= 10000 && salary <= 20000;
    
  • 查询 employees 表中,部门编号不在 90 到 110 之间,或者工资高于 15000 的员工信息

    select * 
    from employees
    where not(department_id >= 90 && department_id <= 110)
    	or salary > 15000;
    或者
    select * 
    from employees
    where (department_id < 90 || department_id > 110)
    	or salary > 15000;
    
4.模糊查询
  • 关键字:like, not like(一般和通配符搭配使用)

  • 通配符:

    • %代表示任意多个字符(包括0个字符)
    • 表示任意单个字符
  • 查询 employees 表中,员工 last_name 中包含字符 a 的员工信息

    select * 
    from employees
    where last_name like '%a%';
    
  • 查询 employees 表中,员工 job_id 中第三个字符为 _ 的员工 last_name 和 job_id

    select last_name, job_id
    from employees
    where job_id like '__\_%'; # 注意将下斜杠转义
    
5. between and not between and
  • 适当使用可以提高语句的简洁度,是包含临界值的。

  • 查询 employees 员工编号在 100 到 120 之间的员工信息

    select *
    from employees
    where employee_id >= 100 and employee_id <= 120;
    可简化为
    select *
    from employees
    where employee_id between 100 and 120;
    
6.in not in
  • 判断某字段的值是否属于in列表中的某一项

  • 查询 employees 表中,工种编号是 it_prog、ad_vp 中的员工 last_name 和工种编号

    select last_name, job_id
    from employees
    where job_id in ('it_prog', 'ad_vp', 'ad_pres');
    
7.is null is not null <=>
  • = <>不能用于判断 null 值而is nullis not null 可以判断 null 值。

  • is null仅仅可以判断 null 值,<=> 既可以判断 null 值,又可以判断普通的数值。、

  • 查询 employees 表中,没有提成的员工 last_name

    select last_name
    from employees
    where commission_pct <=> null;
    
  • 查询 employees 表中,工资为 12000 的员工 last_name 和工资

    select last_name, salary
    from employees
    where salary <=> 12000;
    

希望,带领我进入黑暗中,照亮苍穹。 ——达达乐队《苍穹》

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值