SELECT
employee_id,
name,
MONTH(birth_date) AS birth_month,
YEAR(birth_date) AS birth_year
FROM
tarena.employees
WHERE
employee_id = 8;
DESC tarena.salary;
SELECT
basic + bonus AS PAY
FROM
tarena.salary
WHERE
employee_id = 8
AND
date=20190110;
SELECT
employee_id , name ,
2022 - YEAR(birth_date) AS years_old
FROM
employees
WHERE
employee_id = 8 ;
SELECT
employee_id, (basic+bonus)/2 AS avg_pay
FROM
tarena.salary
WHERE
employee_id = 8
AND
date=20190110;
SELECT
employee_id,name
FROM
tarena.employees
WHERE
employee_id BETWEEN 1 and 10
AND
employee_id % 2 = 0;
SELECT IFNULL(NULL,"AAA");
SELECT IFNULL("nnn","AAA");
SELECT name, IFNULL(homedir, "NO home as HOME_DIR") FROM tarena.user ;
创建 + 删除
CREATE DATABASE gamedb;
CREATE DATABASE IF NOT EXISTS gamedb;
DROP DATABASE gamedb ;
DROP DATABASE IF EXISTS gamedb;
CREATE TABLE IF NOT EXISTS student_info (name char(10), class char(9));
CREATE TABLE student_info (name char(10), class char(9));
DROP TABLE student_info ;
DROP TABLE IF EXISTS student_info ;
CREATE TABLE wage_grade(id int primary key auto_increment, grade char(1), low int, high int);
INSERT INTO
wage_grade(grade,low,high)
VALUES
('A', 5000, 8000),
('B', 8001, 10000),
('C', 10001, 15000),
('D', 15001, 20000),
('E', 20001, 100000);
SELECT 表头名列表 FROM 表1 INNER JOIN 表2 ;
SELECT 表头名列表 FROM 表1 INNER JOIN 表2 ON 连接条件;
SELECT表头名列表
FROM
表1
INNER JOIN
表2
ON
连接条件
[WHERE 筛选条件] | [GROUP BY 分组] | [HAVING 分组后筛选]|[ORDER BY 排序列表]
SELECT
name, dept_name
FROM
tarena.employees
INNER JOIN
tarena.departments
ON
tarena.employees.dept_id = tarena.departments.dept_id
LIMIT 3;
SELECT
name,dept_name
FROM
employees
INNER JOIN
departments
ON
employees.dept_id=departments.dept_id
WHERE
employees.employee_id = 8;
-- 表起别名
SELECT
d.dept_name, e.*
FROM
employees AS e
INNER JOIN
departments AS d
ON
e.dept_id = d.dept_id LIMIT 3 ;
-- 异表具有同名表头,表头前必须加表名
SELECT
name, e.dept_id, d.dept_id, dept_name
FROM
employees AS e
INNER JOIN
departments AS d
ON
e.dept_id = d.dept_id
LIMIT 3;
WHERE 添加筛选条件
AS 别名
YEAR 时间函数取年份
SELECT
e.employee_id, name, date, basic,
bonus, basic+bonus AS sum
FROM
employees AS e
INNER JOIN
salary AS s
ON
e.employee_id = s.employee_id
WHERE
year(s.date)=2018 AND e.employee_id = 11;
GROUP BY 分组
SELECT
s.employee_id, name,
SUM(basic+bonus) AS sum
FROM
employees AS e
INNER JOIN
salary AS s
ON
e.employee_id = s.employee_id
WHERE
YEAR(date)=2018
GROUP BY
s.employee_id LIMIT 3;
升序 ASC
降序 DESC
SELECT
s.employee_id, name,
SUM(basic+bonus) AS sum
FROM
employees AS e
INNER JOIN
salary AS s
ON
e.employee_id = s.employee_id
WHERE
YEAR(date)=2018
GROUP BY
s.employee_id
ORDER BY
sum ASC
LIMIT 6 ;
SELECT
s.employee_id, name,
SUM(basic+bonus) AS sum
FROM
employees AS e
INNER JOIN
salary AS s
ON
e.employee_id = s.employee_id
WHERE
YEAR(date)=2018
GROUP BY
s.employee_id
ORDER BY
sum DESC
LIMIT 6 ;
SELECT
date, employee_id, basic, grade
FROM
salary
INNER JOIN
wage_grade
ON
salary.basic
BETWEEN
wage_grade.low AND wage_grade.high
WHERE YEAR(date)=2018 AND MONTH(date)=12 LIMIT 3;
实现分类分组的功能
BETWEEN AND 确定范围
SELECT
name, date, basic, grade
FROM
employees AS e
INNER JOIN
salary AS s
ON
e.employee_id=s.employee_id
INNER JOIN
wage_grade AS g
ON
s.basic
BETWEEN g.low AND g.high
WHERE
YEAR(date)=2018 AND MONTH(date)=12 LIMIT 3;
SELECT
name, date, basic, grade
FROM
employees AS e
INNER JOIN
salary AS s
ON
e.employee_id=s.employee_id
INNER JOIN
wage_grade AS g
ON
s.basic BETWEEN g.low AND g.high
WHERE
YEAR(date)=2018 AND MONTH(date)=12
LIMIT 6;
SELECT
e.name, e.hire_date, em.birth_date
FROM
employees AS e
INNER JOIN
employees AS em
ON
MONTH(e.hire_date)=MONTH(em.hire_date)
AND
e.employee_id = em.employee_id LIMIT 3;
比较2个表格数据不同
左链接:以左表为标准
右链接:以右表为标准
SELECT
dept_name, name
FROM
departments
LEFT JOIN
employees
ON
departments.dept_id = employees.dept_id
WHERE
name IS NOT NULL LIMIT 3;
SELECT
name,dept_name,employees.dept_id
FROM
departments
RIGHT JOIN
employees
ON
departments.dept_id = employees.dept_id
LIMIT 5;
多个select查询结果输出
表头和数据类型一致
UNION 关键字默认去重
UNION ALL 包含重复项
(SELECT "ABC") UNION (SELECT "QWE");
(SELECT "ABC") UNION ALL (SELECT "QWE");
(SELECT "ABC") UNION ALL (SELECT "ABC");
(SELECT "ABC") UNION (SELECT "ABC");
(SELECT name,birth_date FROM employees WHERE YEAR(birth_date)=1972)
UNION
(SELECT name,birth_date FROM employees WHERE YEAR(birth_date)=1973)
LIMIT 4;
SELECT
name,birth_date
FROM
employees
WHERE
YEAR(birth_date)=1972 OR YEAR(birth_date)>2000;
编写过程由内往外写
SELECT * FROM employees
WHERE
dept_id = (
SELECT
dept_id
FROM
departments
WHERE
dept_name='运维部'
)
LIMIT 3;
SELECT * FROM salary
WHERE
YEAR(date)=2018 AND MONTH(date)=12
AND
basic > (
SELECT basic FROM salary
WHERE YEAR(date)=2018
AND MONTH(date)=12
AND employee_id = 100)
LIMIT 3;
SELECT
dept_id , name
FROM
employees
WHERE
dept_id IN (
SELECT dept_id FROM departments
WHERE dept_name in ('人事部','财务部')
)
LIMIT 3;
SELECT
employee_id, basic, bonus
FROM
salary
WHERE
employee_id IN (
SELECT
employee_id
FROM
employees
WHERE
dept_id = (
SELECT dept_id FROM departments
WHERE dept_name = '人事部'
)
)
LIMIT 3;
SELECT
employee_id, basic, bonus
FROM
salary
WHERE
YEAR(date)=2018 AND MONTH(date)=12
AND
basic = (
SELECT MAX(basic)
FROM salary WHERE YEAR(date)=2018
AND MONTH(date)=12)
AND
bonus = (
SELECT MAX(bonus)
FROM salary
WHERE YEAR(date)=2018
AND MONTH(date) = 12) ;
SELECT
dept_id , dept_name,
employee_id , name , email
FROM (
SELECT
d.dept_name, e.*
FROM
departments AS d
INNER JOIN
employees AS e
ON
d.dept_id = e.dept_id
) AS tmp_table
WHERE dept_id = 3;
SELECT
d.*, (
SELECT
COUNT(name)
FROM
employees AS e
WHERE
d.dept_id = e.dept_id
) AS sum
FROM
departments AS d ;
SELECT
dept_id ,COUNT(name) AS sum
FROM
employees
GROUP BY
dept_id
HAVING
sum < (
SELECT COUNT(name) FROM employees
WHERE dept_id = (
SELECT dept_id FROM departments
WHERE dept_name='开发部'
)
);