SELECT 高级应用

REVIEW

YEAR + MONTH

SELECT 
    employee_id, 
    name, 
    MONTH(birth_date) AS birth_month, 
    YEAR(birth_date) AS birth_year 
FROM 
    tarena.employees 
WHERE 
    employee_id = 8;

SELECT 高级应用_第1张图片

DESC

DESC tarena.salary;

SELECT 高级应用_第2张图片

+ - () / %

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 高级应用_第3张图片

 SELECT 高级应用_第4张图片

 IFNULL

SELECT IFNULL(NULL,"AAA");
SELECT IFNULL("nnn","AAA");


SELECT name, IFNULL(homedir, "NO home as HOME_DIR") FROM tarena.user ;

SELECT 高级应用_第5张图片

SELECT 高级应用_第6张图片

CREATE + DROP

创建 + 删除

CREATE DATABASE gamedb;
CREATE DATABASE IF NOT EXISTS gamedb;

DROP DATABASE gamedb ;
DROP DATABASE IF EXISTS gamedb;

SELECT 高级应用_第7张图片

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 ;

SELECT 高级应用_第8张图片

INSERT

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 高级应用_第9张图片

内连接

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 高级应用_第10张图片

SELECT 
    name,dept_name 
FROM 
    employees 
INNER JOIN 
    departments 
ON 
    employees.dept_id=departments.dept_id 
WHERE 
    employees.employee_id = 8;

SELECT 高级应用_第11张图片

-- 表起别名
SELECT 
    d.dept_name, e.*  
FROM 
    employees AS e  
INNER JOIN 
    departments AS d 
ON 
    e.dept_id = d.dept_id LIMIT 3 ;

SELECT 高级应用_第12张图片

-- 异表具有同名表头,表头前必须加表名
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;

SELECT 高级应用_第13张图片

WHERE + AS

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;

SELECT 高级应用_第14张图片

GROUP BY

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;

SELECT 高级应用_第15张图片

ORDER BY

升序        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 高级应用_第16张图片

非等值链接

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;

SELECT 高级应用_第17张图片

 实现分类分组的功能 

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 高级应用_第18张图片

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 高级应用_第19张图片

 自链接

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;

SELECT 高级应用_第20张图片

外链接

比较2个表格数据不同

左链接:以左表为标准

右链接:以右表为标准

LEFT JOIN

SELECT 
    dept_name, name 
FROM 
    departments 
LEFT JOIN 
    employees 
ON 
    departments.dept_id = employees.dept_id 
WHERE 
    name IS NOT NULL LIMIT 3;

SELECT 高级应用_第21张图片

RIGHT JOIN

SELECT 
    name,dept_name,employees.dept_id  
FROM 
    departments 
RIGHT JOIN 
    employees 
ON 
    departments.dept_id = employees.dept_id 
LIMIT 5;

SELECT 高级应用_第22张图片

全外链接 UNION + UNION ALL

多个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 高级应用_第23张图片

(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 高级应用_第24张图片

子查询(嵌套查询)

编写过程由内往外写

WHERE

SELECT * FROM employees 
WHERE 
    dept_id = (
    SELECT 
        dept_id 
    FROM 
        departments 
    WHERE 
        dept_name='运维部'
    ) 
LIMIT 3;

SELECT 高级应用_第25张图片

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 高级应用_第26张图片

SELECT 
    dept_id , name 
FROM 
    employees 
WHERE 
    dept_id IN (
        SELECT dept_id FROM departments 
        WHERE dept_name in ('人事部','财务部')
    ) 
LIMIT 3;

SELECT 高级应用_第27张图片

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 高级应用_第28张图片

FROM

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 高级应用_第29张图片

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 高级应用_第30张图片

SELECT

SELECT 
    d.*, (
    SELECT 
        COUNT(name) 
    FROM 
        employees AS e 
    WHERE 
        d.dept_id = e.dept_id
    ) AS sum 
FROM 
    departments AS d ;

SELECT 高级应用_第31张图片 

HAVING

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='开发部'
        )
    );

SELECT 高级应用_第32张图片

你可能感兴趣的:(R,or,DB,or,SQL,数据库)