第二章 MySQL 您所在的位置:网站首页 关于心理学的好听的句子 第二章 MySQL

第二章 MySQL

#第二章 MySQL| 来源: 网络整理| 查看: 265

第二章 MySQL-DQL命令 IFNULL()函数的用法:

判断某一列是否为null,第一个参数数目标列,如果是null,那么你想让他返回输出什么内容就传二个参数!

select IFNULL(commission_pct,0) as "结果" from 多个字段查询也可以嵌套如下: select concat(first_name,",",last_name,",",INFNUL(commission_pct,0)) as "结果" from employees; 条件查询where关键字 格式:select 查询列表 from 表名 where 筛选条件;按条件表达式筛选: 按条件表达式筛选小与符号 < 等于符号 = 不等于符号 != 不等于符号 案例1:查询工资大于12000的员工信息 select * from employees where salary > 12000; 案例2:查询部门编号不等于90号的员工名和部门编号 select first_name, department_id from employees where department_id != 90; 案例3:按逻辑表达示筛选 and or not 查询工资在10000 到20000之间的员工名和工资和奖金 select first_name,salary,commission_pct from employees where salary >= 10000 and salary 15000; 或者这样写:用not关键字,not关键字就是java中取反逻辑表达式: select * from employees where not (department_id>=90 and department_id15000; 模糊查询 Like的用法

模糊查询关键字有以下几个: Like, between and, in, is null, is not null,

案例 1. 查询员工名中包含字符a的员工信息 select * from employees where first_name like '%a%'; 案例 2. 查询员工名中第三个字符为n,第五个字符为l的员工名和工资(用通配符下划线表示第几个字符) select last_name,salary from employees where last_name like '__n_l%'; 案例 3. 查询员工表中 第二个字符为下划线的 员工名 这种写法是用转义符来区分 select last_name from employees where last_name like '_\_%'; 或者用关键字escape来指定转义符,这样的话你的转义符就可以随意定义成$或者#等各种不一样的符号, 都他认为是转移符: select last_name from employees where last_name like '_$_%' escape '$' between and 的用法 查询员工编号在100到120之间的员工信息 select * from employees where employee_id between 100 and 120; In()的用法 案例1.查询员工表(employess)中的工种编号是IT_PROG,AD_VP,AD_PRES中的一个员工名和工种编号; 写法一: select last_name,job_id from employees where job_id = 'IT_PROT' OR job_id = 'AD_VP' OR job_id = 'AD_PRES'; 写法二:(这里使用的是in关键字) select last_name,job_id from employees where job_id in('IT_PROT','AD_VP','AD_PRES');

总结:in关键字主要是判断某个字段的值是否属于in列表中的某一项

is null 用法 案例1. 查询员工表中,没有奖金的,员工名和奖金率(其实就是等于null的) select first_name,commission_pct from employees where commission_pct is null; 案例2. 查询员工表中,有奖金的,员工名和奖金率(其实就是不等于null的) select first_name,commission_pct from employees where commission_pct is not null ;

以下是练习题

查询员工号为176的员工的姓名和部门号和年薪 select concat(last_name,first_name) as "姓名", department_id as "部门号", salary*12*(1+ifnull(commission_pct,0)) as "年薪" from employees where employee_id = 176; 注意:年薪 = 月薪*12+(1+奖金率),而奖金率字段有可能会null所以用上了ifnull()函数 查询工资不在5000到12000的员工的姓名和工资 并且按升序排序 select last_name,salary from employees where not salary between 5000 and 12000 order by salary asc ; 查询部门id为20号或50号部门的员工姓名和部门号 select last_name,department_id from employees where department_id in (50,20); 查询员工中没有管理者的员工姓名及job_id select last_name,job_id from employees where manager_id is null; 查询员工表中有奖金的员工姓名,工资和奖金级别 select last_name,salary,commission_pct from employees where commission_pct is not null ; 查询员工姓名的第三个字母是a的员工姓名; select last_name from employees where last_name like '__a%' 查询员工表中有字母a和e的员工姓名; select last_name from employees where last_name like '%ae%'; 查询员工表中first_name 以 e结尾的员工信息 select * from employees where first_name like '%e' 查询员工表中部门编号在80 - 100之间的姓名,职位 select last_name,job_id from employees where department_id between 80 and 100; 查询员工表中的 mannger_id 是 100,101,110的员工姓名,职位 select last_name,job_id from employees where manager_id in (100,101,110);

以上内容较多,需要静心新来多敲几遍,方可记住



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有