lyt经典版MySQL基础——进阶6:连接查询-sql99语法-内连接、外连接、交叉连接
短信预约 信息系统项目管理师 报名、考试、查分时间动态提醒
1 #二、sql99语法
2
21
22 #(一)内连接
23
42
43 #1.等值连接
44 #案例1:查询员工名、部门名
45 SELECT last_name,department_name
46 FROM employees e
47 INNER JOIN departments d
48 ON e.department_id=d.department_id;
49 #案例2:查询名字中包含e的员工名和工种名(添加筛选)
50 SELECT last_name,job_title
51 FROM employees e
52 INNER JOIN jobs j
53 ON e.job_id=j.job_id
54 WHERE last_name LIKE ‘%e%‘;
55 #案例3:查询部门个数>3的城市名和部门个数(分组+筛选)
56 #(1)查询每个城市的部门个数
57 #(2)在(1)结果上筛选满足条件的
58 SELECT city,COUNT(*) ‘部门个数‘
59 FROM locations l
60 INNER JOIN departments d
61 ON l.location_id=d.location_id
62 GROUP BY city
63 HAVING 部门个数>3;
64 #案例4:查询哪个部门的部门员工个数>3的部门名和员工个数,
65 #并按个数降序(添加排序)
66 SELECT department_name,COUNT(*) ‘员工个数‘
67 FROM departments d
68 INNER JOIN employees e
69 ON d.department_id=e.department_id
70 GROUP BY e.department_id
71 HAVING 员工个数>3
72 ORDER BY 员工个数 DESC;
73 #案例5:查询员工名、部门名、工种名,并按部门名降序
74 SELECT last_name,department_name,job_title
75 FROM employees e
76 INNER JOIN departments d ON e.department_id=d.department_id
77 INNER JOIN jobs j ON e.job_id=j.job_id
78 ORDER BY job_title DESC;
79
80 #2.非等值连接
81 #查询员工的工资级别
82 SELECT salary,grade_level
83 FROM employees
84 JOIN job_grades
85 ON salary BETWEEN lowest_sal AND highest_sal;
86 #查询工资级别的个数>2的个数,并且按工资级别降序
87 SELECT grade_level,COUNT(*) ‘个数‘
88 FROM employees
89 JOIN job_grades
90 ON salary BETWEEN lowest_sal AND highest_sal
91 GROUP BY grade_level
92 HAVING 个数>2
93 ORDER BY grade_level DESC;
94
95 #3.自连接
96 #查询员工的名字、上级的名字
97 SELECT e.last_name,m.last_name
98 FROM employees e
99 JOIN employees m ON e.manager_id=m.employee_id;
100 #查询姓名中包含字符k的员工的名字、上级的名字
101 SELECT e.last_name,m.last_name
102 FROM employees e
103 JOIN employees m ON e.manager_id=m.employee_id
104 WHERE e.last_name LIKE ‘%k%‘;
105
106 #(二)外连接
107
120 #引入:查询男朋友不在男神表的女神名
121 SELECT * FROM beauty;
122 SELECT * FROM boys;
123 #左外连接
124 SELECT b.name,bo.*
125 FROM beauty b
126 LEFT OUTER JOIN boys bo
127 ON b.boyfriend_id=bo.id
128 WHERE bo.id IS NULL;
129 #右外连接
130 SELECT b.name,bo.*
131 FROM boys bo
132 RIGHT OUTER JOIN beauty b
133 ON b.boyfriend_id=bo.id
134 WHERE bo.id IS NULL;
135
136 #案例1:查询哪个部门没有员工
137 #左外
138 SELECT d.*,e.employee_id
139 FROM departments d
140 LEFT OUTER JOIN employees e
141 ON d.department_id=e.department_id
142 WHERE employee_id IS NULL;
143 #右外
144 SELECT d.*,e.employee_id
145 FROM employees e
146 RIGHT OUTER JOIN departments d
147 ON d.department_id=e.department_id
148 WHERE employee_id IS NULL;
149
150 #全外
151 USE girls;
152 SELECT b.*,bo.*
153 FROM bauty b
154 FULL OUTER JOIN boys bo
155 ON b.boyfriend_id=bo.id;
156
157 #交叉连接
158 SELECT b.*,bo.*
159 FROM beauty b
160 CROSS JOIN boys bo;
161
162 #sql92 和 sql99 PK
163
lyt经典版MySQL基础——进阶6:连接查询-sql99语法-内连接、外连接、交叉连接
原文地址:https://www.cnblogs.com/yanwuliu/p/13373333.html
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341