MySQL 基础
#1.查询常量
SELECT 100;
SELECT last_name
FROM employees
;
#4.查询多个字段
SELECT last_name
,email
,employee_id
FROM employees;
#5.查询所有字段
SELECT * FROM employees;
F12 :对齐格式
SELECT
last_name
,
email
,
job_id
#6.查询函数(调用函数,获取返回值)
SELECT DATABASE();
SELECT VERSION();
SELECT USER();
#7.起别名
#方式一:使用as关键字
SELECT USER() AS 用户名;
SELECT last_name AS ‘姓 名’ FROM employees;
#方式二:空格
#八,
– 需求 : 查询 first_name 和 last_name 拼接成的全名,最终起别名为:姓 名
SELECT CONCAT(first_name,last_name) AS ‘姓 名’ FROM employees;
SELECT first_name,last_name FROM employees;
#9. distinct的使用 去重
#需求: 查询员工涉及到的部门编号有哪些
SELECT DISTINCT department_id FROM employees;
#10.查看表的结构
DESC employees;
SHOW COLUMNS FROM employees;
#进阶二:条件查询
/*
语法:
select 查询列表
frmo 表名
where 筛选条件
执行顺序:
select last_name,first_name from emp where salary > 20000;
*/
#1.按关系表达式筛选
#案例一,查询部门编号不是100的员工信息
SELECT * FROM employees WHERE department_id <> 100;
#案例二: 查询工资<15000的姓名,工资
SELECT CONCAT(last_name,first_name) AS ‘姓名’ FROM employees WHERE salary<15000;
#二. 按逻辑表达式筛选
#案例1:查询部门编号不是 50-100之间员工姓名,部门编号,邮箱
#方式一:
SELECT last_name,department_id,email FROM employees WHERE department_id<50 OR department_id>100;
#方式二:
SELECT last_name,department_id,email FROM employees WHERE NOT(department_id>=50 AND department_id<=100);
#案例2: 查询奖金率>0.03 或者 员工编号在60-110之间的员工信息
SELECT * FROM employees WHERE commission_pct>0.03 OR (employee_id>=60 AND employee_id<=110);
#1.like
#案例一:查询姓名中包含最后一个字符为e的员工的信息
SELECT * FROM employees WHERE last_name LIKE ‘%a%’;
#案例二:查询姓名中包含第一个字符为e的员工的信息
SELECT * FROM employees WHERE last_name LIKE ‘e%’;
#案例三:查询姓名中包含第三个字符为e的员工的信息
SELECT * FROM employees WHERE last_name LIKE ‘___x%’;
#案例四:查询姓名中包含第二个字符为_的员工的信息
SELECT * FROM employees WHERE last_name LIKE ‘__%’;
SELECT * FROM employees WHERE last_name LIKE ‘#%’ ESCAPE ‘#’;
#2. in
/*
功能:查询某字段的值是否属于指定的列表之内
a in(常量1,常量2.常量3…)
a not in(常量1,常量2.常量3…)
in/ not in
*/
#案例一:查询部门编号是 30/50/90的员工名和部门编号
SELECT last_name,department_id FROM employees
WHERE department_id IN(30,50,90);
#案例二:查询工种编号不是SH_CLERK 或 IT_PROG的员工信息
SELECT * FROM employees WHERE (job_id != ‘SH_CLERK’) AND (job_id != ‘IT_PROG’);
SELECT * FROM employees WHERE job_id NOT IN(‘SH_CLERK’,‘IT_PROG’);
#3.between and
/*
功能:判断某个字段的值是否介于xx之间
between and / not between and
*/
#案例一: 查询部门编号是30-90之间的部门编号,员工姓名
SELECT department_id,last_name FROM employees
WHERE department_id>=30 AND department_id<=90;
SELECT department_id,last_name FROM employees
WHERE department_id BETWEEN 30 AND 90;
#案例二: 查询年薪不是100000-200000直接员工的姓名,工资,年薪
SELECT last_name,salary,salary12(IFNULL(commission_pct,0)+1) 年薪
FROM employees
WHERE salary12(IFNULL(commission_pct,0)+1) NOT BETWEEN 100000 AND 200000;
SELECT last_name,salary,salary12(IFNULL(commission_pct,0)+1) AS 年薪
FROM employees
WHERE salary12(IFNULL(commission_pct,0)+1)<100000 OR salary12(IFNULL(commission_pct,0)+1)>200000;
#4. is null / is not null
#案例一:查询没有奖金的员工信息
SELECT * FROM employees WHERE commission_pct IS NULL;
#案例一:查询有奖金的员工信息
SELECT * FROM employees WHERE commission_pct IS NOT NULL;
#1.查询工资大于 12000 的员工姓名和工资
SELECT last_name,salary FROM employees WHERE salary > 12000;
#2.查询员工号为176的员工姓名和部门号和年薪
SELECT last_name,department_id,salary12(IFNULL(commission_pct,0)+1) 年薪
FROM employees
WHERE employee_id = 176;
#3.选择工资不在 5000 到 12000 的员工姓名 和 工资
SELECT last_name,salary FROM employees
WHERE salary NOT BETWEEN 5000 AND 12000;
#方式二:
SELECT last_name,salary FROM employees
WHERE NOT(salary>=5000 AND salary<=12000);
#方式三:
SELECT last_name,salary FROM employees
WHERE salary<5000 OR salary>12000;
#4.选择在20 或 50 号部门工作的员工姓名和部门号
SELECT last_name,department_id FROM employees
WHERE department_id=20 OR department_id=50;
#方式二
SELECT last_name,department_id FROM employees
WHERE department_id IN(20,50);
#5.选择公司中没有管理者的员工姓名及 job_id
SELECT last_name,job_id FROM employees
WHERE manager_id IS NULL;
#6. 选择公司中有奖金的员工姓名,工资和奖金级别
SELECT last_name,salary,commission_pct FROM employees
WHERE commission_pct IS NOT NULL;
#7.选择员工姓名的第三个字母是a的员工姓名
SELECT last_name FROM employees
WHERE last_name LIKE ‘__a%’;
#8.选择姓名中有字母 a 和 e 的 员工姓名
SELECT last_name FROM employees
WHERE last_name LIKE ‘%a%’ AND last_name LIKE ‘%e%’;
#9.显示出表 employees 表中 first_name 以‘e’结尾的员工信息
SELECT * FROM employees
WHERE first_name LIKE ‘%e’;
#10.显示出表 employees 部门编号在 80-100 之间的姓名 职位
SELECT last_name,job_id FROM employees
WHERE department_id BETWEEN 80 AND 100;
#方式二:
SELECT last_name,job_id FROM employees
WHERE department_id>=80 AND department_id<=100;
#11.显示出表 employees 的 manager_id 是100,101,110的员工姓名,职位
SELECT last_name,job_id FROM employees
WHERE manager_id IN(100,101,110);
SELECT last_name,job_id FROM employees
WHERE manager_id=100 OR manager_id=101 OR manager_id=110;