06.1sql92-连接查询-内连接-等值连接——MySQL

--  我们可以为表起别名
/*
	注意:如果为表起了别名,则查询字段就不能用原来的表名去限定,而应该用别名限定,否则报错
			因为:按照执行顺序,会先执行from,起了别名之后,表名就会被别名覆盖
*/

-- 案例1:查询员工名,工种号,工种名
SELECT e.last_name,e.job_id,j.job_title
FROM  employees e,jobs j 
WHERE e.job_id=j.job_id;


-- ——————————加筛选————————————————————————
-- 案例1:查询有奖金的员工名和部门名
SELECT last_name,department_name,commission_pct
FROM employees e,departments d
WHERE e.department_id=d.department_id
AND e.commission_pct IS NOT NULL;

-- 案例2:城市名中第二个字符为o的对应的部门名和城市名
SELECT department_name,city
FROM departments d,locations l
WHERE d.location_id=l.location_id
AND city LIKE "_o%";
			
-- ——————————加分组———————————————————————— 
-- 案例1:查询每个城市的部门个数
SELECT COUNT(department_id) 部门个数,city
FROM departments d,locations l
WHERE d.location_id=l.location_id
GROUP BY city;


-- 案例2:查询有奖金的每个部门的部门名和部门领导编号和该部门的最低工资
SELECT d.department_name,d.manager_id,MIN(salary)
FROM employees e,departments d
WHERE d.department_id=e.department_id
AND e.commission_pct IS NOT NULL
ORDER BY d.department_name;

-- ——————————加排序————————————————————————————
-- 案例1:查询每个工种的工种名和员工的个数,并且按员工个数降序
SELECT job_title, COUNT(employee_id)
FROM jobs j,employees e
WHERE j.job_id=e.job_id
GROUP BY job_title
ORDER BY COUNT(employee_id) DESC;



-- —————————3个表连接———————————————————————————— 
-- 案例1:查询员工名,部门名和所在的城市
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;














你可能感兴趣的:(MySQL数据库)