查询所用到的数据库创建脚本myemployees.sql,执行脚本即可生成对应的数据库及表格:
CSDN连接:
用到的数据库构建脚本my employees.sql、boys.sql…
1. 基础查询
"""1. 查询表中的单个字段"""
USE myemployees;
SELECT last_name FROM employees;
"""2. 查询表中的多个字段"""
SELECT last_name, salary, email FROM employees;
"""3. 查询表中的所有字段"""
SELECT * FROM employees;
"""4. 查询常量值"""
SELECT 100;
SELECT 'john';
"""5. 查询表达式"""
SELECT 100%98;
"""6. 查询函数"""
SELECT VERSION();
"""7. 为字段起别名"""
SELECT 100%98 AS RESULT;
SELECT last_name AS 姓, first_name AS 名 FROM employees;
SELECT last_name 姓, first_name 名 FROM employees;
SELECT salary AS "output" FROM employees;
"""8. 去重"""
SELECT DISTINCT department_id FROM employees;
"""9. +号的作用:运算符,不能用于拼接,强转字符进行算术运算,若转换失败则转换成0"""
SELECT
last_name+first_name AS 姓名
FROM
employees;
"""10. 拼接"""
SELECT CONCAT(`a`, `b`, `c`) AS RESULTS
"""11. 案例"""
SELECT
IFNULL(字段名, new_value) AS new_name,
comission_pct
FROM
employees;
SELECT
CONCAT(`first_name`, ',', `last_name`, ',', `job_id`, ',', IFNULL(comission_pct, 0)) AS OUTPUT
FROM
employees;
2. 条件查询
'''一. 按条件表达式筛选,条件运算符:> < = <> != >= <='''
SELECT * FROM employees WHERE salary>12000;
SELECT
last_name, department_id
FROM
employees
WHERE
department_id!=90;
'''二. 按逻辑表达式筛选,条件运算符:&& || ! and or not'''
SELECT last_name, salary, commision_pct FROM employees WHERE salary>=10000 AND salary<=20000;
SELECT * FROM employees WHERE department_id<90 OR department_id>110 OR salary>15000;
SELECT * FROM employees WHERE NOT(department_id>=90 AND department_id<=110) OR salary>15000;
'''三. 模糊查询:like between...and in is...null'''
'''like'''
SELECT * FROM employees WHERE last_name LIKE '%a%';
SELECT last_name, salary FROM employees WHERE last_name LIKE '__e_a%';
SELECT last_name FROM employees WHERE last_name LIKE '_\_%';
SELECT last_name FROM employees WHERE last_name LIKE '_$_%' ESCAPE '$
';
'''between and'''
SELECT * FROM employees WHERE employee_id BETWEEN 100 AND 120;
'''in'''
SELECT last_name, job_id FROM employees WHERE job_id in ('IT_PROG', 'AD_VP', 'AD_PRES');
'''is null'''
SELECT last_name, comission_pct FROM employees WHERE comission_pct IS NULL;
'''安全等于<=>'''
SELECT last_name, comission_pct FROM employees WHERE comission_pct <=> NULL;
SELECT last_name, salary FROM employees WHERE salary <=> 12000;
SELECT last_name, department_id, salary*12*(1+IFNULL(comission_pct, 0)) AS 年薪 FROM employees;
3. 排序查询
SELECT * FROM employees ORDER BY salary DESC;
SELECT * FROM employees WHERE department_id>=90 ORDER BY hiredate DESC;
SELECT *, salary*12*(1+IFNULL(commision_pct, 0)) 年薪 FROM employees ORDER BY salary*12*(1+IFNULL(commision_pct, 0)) DESC;
SELECT *, salary*12*(1+IFNULL(commision_pct, 0)) 年薪 FROM employees ORDER BY 年薪 DESC;
SELECT LENGTH(last_name) 字节长度, last_name, salary FROM employees ORDER BY LENGTH(last_name) DESC;
SELECT * FROM employees ORDER BY salary ASC, employee_id DESC;
4. 常见函数
'''1. 单行函数'''
SELECT LENGTH('john');
SELECT CONCAT(last_name, '_', first_name) full_name FROM employees;
SELECT UPPER('john');
SELECT CONCAT(UPPER(last_name), '_', LOWER(first_name)) full_name FROM employees;
"""索引位置从1开始"""
SELECT SUBSTR('李莫愁爱上了陆展元', 6) LOVER;
SELECT SUBSTR('李莫愁爱上了陆展元', 1, 3) out_put;
SELECT INSTER('杨不悔爱上了殷六侠', '殷六侠') AS out_put;
SELECT TRIM(' 张翠山。 ') AS out_put;
SELECT TRIM('a' FROM 'aaaa张翠山aaaa') AS out_put;
SELECT LPAD('殷素素', 10, '*') AS out_put;
SELECT RPAD('殷素素', 12, 'ab') AS out_put;
SELECT REPLACE('张无忌周芷若爱上了周芷若', '周芷若', '赵敏') AS out_put;
SELECT ROUND(1.65);
SELECT ROUND(1.567, 2);
SELECT CEIL(1.002);
SELECT FLOOR(-9.99);
SELECT TRUNCATE(1.6999, 1);
SELECT MOD(10, 3);
SELECT NOW();
SELECT CURDATE();
SELECT CURTIME();
SELECT YEAR(NOW()) 年;
SELECT YEAR('1991-1-1') 年;
SELECT YEAR(hirdate) 年 FROM employees;
SELECT MONTH(NOW()) 月;
SELECT MONTHNAME(NOW()) 月;
SELECT str_to_date('1998-3-2', '%Y-%c-%d') AS output;
SELECT * FROM employees WHERE hirdate=STR_TO_DATE('4-3 1992', '%c-%d %Y');
SELECT DATE_FORMAT(NOW(), '%y年%m月%d日') AS output;
SELECT last_name, DATE_FORMAT(hirdate, '%m月/%d日 %y年') 入职日期 FROM employees WHERE commision_pct IS NOT NULL;
SELECT VERSION();
SELECT DATABASE();
SELECT USER();
SELECT IF(10>5, '大', '小');
SELECT last_name, comission_pct, IF(comission_pct IS NULL, '没奖金呵呵', '有奖金嘻嘻') 备注 FROM employees;
SELECT salary 原始工资, department_id,
CASE department_id
WHEN 30 THEN salary*1.1
WHEN 40 THEN salary*1.2
WHEN 50 Then salary*1.3
ELSE salary
END AS 新工资
FROM employees;
SELECT salary,
CASE
WHEN salary>20000 THEN 'A'
WHEN salary>15000 THEN 'B'
WHEN salary>10000 THEN 'C'
ELSE 'D'
END AS 工资级别
FROM employees;
'''2. 分组函数/统计函数'''
SELECT SUM(salary) FROM employees;
SELECT AVG(salary) FROM employees;
SELECT MAX(salary) FROM employees;
SELECT MIN(salary) FROM employees;
SELECT COUNT(salary) FROM employees;
SELECT SUM(salary) 和, AVG(salary) 均值, MAX(salary) 最大, MIN(salary) 最小, COUNT(salary) 个数 FROM employees;
SELECT SUM(DISTINCT salary), SUM(salary) FROM employees;
SELECT COUNT(DISTINCT salary), COUNT(salary) FROM employees;
SELECT COUNT(*) FROM employees;
SELECT COUNT(1) FROM employees;
SELECT AVG(salary), employee_id FROM employees;
5. 分组查询
SELECT MAX(salary), job_id FROM employees GROUP BY job_id;
SELECT COUNT(*), location_id FROM departments GROUP BY location_id;
SELECT AVG(salary), department_id FROM employees WHERE email LIKE '%a%' GROUP BY department_id;
SELECT MAX(salary), manager_id FORM employees WHERE comission_pct IS NOT NULL GROUP BY manager_id;
SELECT COUNT(*) num, department_id FROM employees GROUP BY department_id HAVING COUNT(*)>2;
SELECT MAX(salary), job_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id
HAVING MAX(salary)>12000;
SELECT MIN(salary), manager_id
FROM employees
WHERE manager_id>102
GROUP BY manager_id
HAVING MIN(salary)>5000;
SELECT COUNT(*), LENGTH(last_name) len_name
FROM employees
GROUP BY LENGTH(last_name)
HAVING COUNT(*)>5;
SELECT AVG(salary), department_id, job_id
FROM employees
GROUP BY department_id, job_id;
SELECT AVG(salary), department_id, job_id
FROM employees
WHERE department_id IS NOT NULL
GROUP BY department_id, job_id
ORDER BY AVG(salary) DESC;
6. 连接查询
SELECT * FROM beauty;
SELECT * FROM boys;
SELECT NAME, boyName FROM beauty, boys;
SELECT NAME, boyName
FROM beauty, boys
WHERE beauty.boyfriend_id=boys.id;
SELECT last_name, department_name
FROM employees, departments
WHERE employees.`department_id`=departments.`department_id`;
SELECT E.last_name, E.job_id, J.job_title
FROM employees AS E, jobs J
WHERE E.`job_id`=J.`job_id`;
SELECT last_name, department_name
FROM employees e, departments d
WHERE e.`department_id`=d.`department_id`
AND e.`commision_pct` IS NOT NULL;
SELECT department_name, city
FROM departments a, locations l
WHERE d.`location_id`=l.`location_id`
AND city LIKE '_o%';
SELECT COUNT(*) 个数, city
FROM departments d, locations l
WHERE d.`location_id`=l.`location_id`
GROUP BY city
SELECT department_name, d.manager_id, MIN(salary)
FROM departments d, employees e
WHERE d.`department_id`=e.`department_id`
AND commission_pct IS NOT NULL
GROUP BY department_name, d.manager_id
SELECT job_title, COUNT(*)
FROM employees e, jobs j
WHERE e.`job_id`=j.`job_id`
GROUP BY job_title
ORDER BY COUNT(*) DESC;
SELECT last_name, department_name, city
FROM employees e, departments d, locations l
WHERE e.`department_id`=d.`department_id`
AND d.`location_id`=l.`location_id`;
SELECT salary, grade_level
FROM employees e, job_grades g
WHERE e.salary BETWEEN g,`lowest_salary` AND g.`highest_salary`;
SELECT .last_name, e.employee_id, m.employee_id, m.last_name
FROM employees e, employees m
WHERE e.`manager_id`=m.`employee_id`
SELECT last_name, department_name
FROM employees e
INNER JOIN departments d
ON e.`deparetment_id`=d.`department_id`;
SELECT last_name, job_title
FROM employees e
INNER JOIN jobs j
ON e.`job_id`=d.`job_id`
WHERE e.`last_name` LIKE '%e%';
SELECT city, COUNT(*) 部门个数
FROM departments d
INNER JOIN locations l
ON d.`location_id`=l.`location_id`
GROUP BY city
HAVING COUNT(*)>3;
SELECT COUNT(*), department_id
FROM employees e
INNER JOIN departments d
ON e.`deparetment_id`=d.`department_id`
GROUP BY department_name
HAVING COUNT(*)>3
ORDER BY COUNT(*) DESC;
SELECT last_name, department_name, job_title
FROM employees e
INNER JOIN department_d ON e.`deparetment_id`=d.`department_id`
INNER JOIN jobs j ON e.`job_id`=d.`job_id`
ORDER BY department_name DESC;
SELECT salary, grade_level
FROM employees e
JOIN job_grades g
ON e.`salary` BETWEEN g.`lowest_sal` AND g.`highest_sal`;
SELECT COUNT(*), grade_level
FROM employees e
JOIN job_grades g
ON e.`salary` BETWEEN g.`lowest_sal` AND g.`highest_sal`;
HAVING COUNT(*)>20
GROUP BY grade_level DESC;
SELECT e.last_name, m.last_name
FROM employees e
INNER JOIN employees m
ON e.`manager_id`=m.`employee_id`;
SELECT b.name, bo.
FROM beauty b
LEFT OUTER JOIN boys bo
ON b.`boyfriend_id`=bo.`id`
WHERE bo.`id` IS NULL;
SELECT d.*, e.employee_id
FROM departments d
LEFT OUTER JOIN employees e
ON d.`deparetment_id`=e.`department_id`
WHERE e.`employee_id` IS NULL;
SELECT d.*, e.employee_id
FROM employees e
RIGHT OUTER JOIN departments d
ON d.`deparetment_id`=e.`department_id`
WHERE e.`employee_id` IS NULL;
USE girls;
SELECT b.*, bo.*
FROM beauty b
FULL OUTER JOIN boys bo
ON b.`boyfriend_id`=bo.`id`;
SELECT b.*, bo.*
FROM beauty b
CROSS JOIN boys bo;
SELECT b.id, b.name, bo.*
FROM beauty b
LEFT OUTER JOIN boys bo
ON b.`boyfriend_id`=bo.`id`
WHERE b.`id`>3
SELECT city, d.*
FROM departments d
RIGHT OUTER JOIN locations l
ON d.`location_id`=l.`location_id`
WHERE d.`department_id` IS NULL;
SELECT e.*, d.department_name
FROM departments d
LEFT JOIN employees e
ON d.`deparetment_id`=e.`department_id`
WHERE d.`deparetment_name` IN('SAL', 'IT');
7. 子查询
SELECT *
FROM employees
WHERE salary>(
SELECT salary
FROM employees
WHERE last_name='Abel'
);
SELECT last_name, job_id, salary
FROM employees
WHERE job_id=(
SELECT salary
FROM employees
WHERE employee_id=141
) AND salary>(
SELECT salary
FROM employees
WHERE employee_id=143
)
SELECT last_name, job_id, salary
FROM employees
WHERE salary=(
SELECT MIN(salary)
FROM employees
);
SELECT MIN(salary), department_id
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
SELECT MIN(salary)
FROM employees
WHERE department_id=50
);
SELECT last_name
FROM employees
WHERE department_id IN(
SELECT department_id
FROM departments
WHERE location_id IN(1400, 1700)
);
SELECT last_name, employee_id, job_id, salary
FROM employees
WHERE salary<ANY(
SELECT DISTINCT salary
FROM employees
WHERE job_id='IT_PROG'
) AND job_id<>'IT_PROG';
SELECT last_name, employee_id, job_id, salary
FROM employees
WHERE salary<(
SELECT MIN(salary)
FROM employees
WHERE job_id='IT_PROG'
) AND job_id<>'IT_PROG';
SELECT *
FROM employees
WHERE (employee_id, salary)=(
SELECT MIN(employee_id), MAX(salary)
FROM employees
)
SELECT *
FROM employees
WHERE employee_id=(
SELECT min(employee_id)
FROM employees
)AND salary=(
SELECT MAX(salary)
FROM employees
)
SELECT d.*, (
SELECT COUNT(*)
FROM employees e
WHERE d.`deparetment_id`=e.`department_id`
)
FROM departments d;
SELECT (
SELECT department_name
FROM departments d
INNER JOIN employees e
ON d.`deparetment_id`=e.`department_id`
WHERE e.`employee_id`=102
) 部门名;
SELECT sg_dep.*, g.`grade_level`
FROM (
SELECT AVG(salary) ag, department_id
FROM employees
GROUP BY department_id;
) ag_dep
INNER JOIN job_grades g
ON ag_dep.ag BETWEEN lowest_sal AND highest_sal;
SELECT department_name
FROM employees e
WHERE EXISTS(
SELECT *
FROM departments d
WHERE d.`deparetment_id`=e.`department_id`
);
SELECT bo.*
FROM boys bo
WHERE bo.id NOT IN(
SELECT boyfriend_id
FROM beauty
);
SELECT bo.*
FROM boys bo
WHERE NOT EXISTS(
SELECT boyfriend_id
FROM beauty b
WHERE bo.`id`=b.`boyfriend_id`
);
8. 分页查询
SELECT * FROM employee LIMIT 0, 5;
SELECT * FROM employee LIMIT 5;
SELECT * FROM employee LIMIT 10, 15;
SELECT *
FROM employees
WHERE commission_pct IS NOT NULL
ORDER BY salary DESC
LIMIT 10;
9. 联合查询
SELECT * FROM employees WHERE email LIKE '%a%' OR department_id>90;
SELECT * FROM employees WHERE email LIKE '%a%'
UNION
SELECT * FROM employees WHERE department_id>90;
SELECT id, cname, csex FROM t_ca WHERE csex='男'
UNION
SELECT t_id, tName, tGender FROM t_ua WHERE tGender='male';
|