w3resource_MySQL练习:Joins
w3resource_MySQL练习题:Joins
-- 1. 自然连接,MySQL自己判断多表的连接条件,分为内/外自然连接 select location_id, street_address, city, state_province, country_name from locations NATURAL JOIN countries -- 2. 多表连接,在from里连接多张表,通过where进行匹配 select l.location_id, l.street_address, l.city, l.state_province, c.country_name from locations as l, countries as c where l.country_id=c.country_id
-- 1. 多表连接 select e.first_name, e.last_name, e.department_id, d.department_name from employees as e, departments as d where e.department_id=d.department_id -- 2. 左连接(显然employees表数量远大于departments表) select e.first_name, e.last_name, e.department_id, d.department_name from employees as e left join departments as d on e.department_id=d.department_id
-- 1. 多表连接 select e.first_name, e.last_name, e.job_id, e.department_id, d.department_name from employees as e, departments as d, locations as l where e.department_id=d.department_id and d.location_id=l.location_id and l.city='London' -- 2. 左连接 select e.first_name, e.last_name, e.job_id, e.department_id, d.department_name from employees as e left join departments as d on e.department_id=d.department_id left join locations as l on d.location_id=l.location_id where l.city='London'
-- 1. 使用多表连接 select e1.employee_id, e1.last_name, e2.employee_id, e2.last_name from employees as e1, employees as e2 where e1.manager_id=e2.employee_id -- 2. 使用内连接 SELECT e.employee_id 'Emp_Id', e.last_name 'Employee', m.employee_id 'Mgr_Id', m.last_name 'Manager' FROM employees e join employees m ON (e.manager_id = m.employee_id);
-- 1. 通过where进行筛选
select first_name, last_name, hire_date
from employees
where hire_date>(
select hire_date from employees where last_name='Jones'
)
-- 2. 内连接
SELECT e.first_name, e.last_name, e.hire_date
FROM employees e
JOIN employees davies
ON (davies.last_name = 'Jones')
WHERE davies.hire_date < e.hire_date;
-- 要点:多表连接后,在新表做group by select d.department_name, count(*) from employees as e, departments as d where e.department_id=d.department_id group by e.department_id
-- 要点:datediff() SELECT employee_id, j.job_title, DATEDIFF(end_date, start_date) FROM job_history NATURAL JOIN jobs AS j WHERE department_id = 90;
-- 要点:以departments表为左表 select d.department_id, d.department_name, e.first_name from departments as d left join employees as e won d.manager_id=e.employee_id
-- 要点:以departments表为主表 select d.department_name, e.first_name, l.city from departments as d left join employees as e on d.manager_id=e.employee_id left join locations as l on d.location_id=l.location_id
-- 要点:group by分组计算组内均值 select job_id, avg(salary) from employees group by job_id
-- 要点:多表连接 SELECT job_title, first_name, salary-min_salary 'Salary - Min_Salary' FROM employees NATURAL JOIN jobs;
-- 要点:多表连接 select j.* from job_history as j, employees as e where j.employee_id=e.employee_id and e.salary>10000
-- 要点:datediff select e.first_name, e.last_name, e.hire_date, e.salary, datediff(now(), (e.hire_date)) as exp from departments as d, employees as e where d.manager_id=e.employee_id and datediff(now(), (e.hire_date))>15
w3resource_MySQL练习:Joins的更多相关文章
- SQL Tuning 基础概述07 - SQL Joins
N多年之前,刚刚接触SQL的时候,就被多表查询中的各种内连接,外连接,左外连接,右外连接等各式各样的连接弄的晕头转向. 更坑的是书上看到的各种表连接还有两种不同的写法, 比如对于表A,表B的查询 1, ...
- Part 7 Joins in sql server
Joins in sql server Advanced or intelligent joins in sql server Self join in sql server Different wa ...
- [HIve - LanguageManual] Joins
Hive Joins Hive Joins Join Syntax Examples MapJoin Restrictions Join Optimization Predicate Pushdown ...
- 转载: C#: Left outer joins with LINQ
I always considered Left Outer Join in LINQ to be complex until today when I had to use it in my app ...
- The dplyr package has been updated with new data manipulation commands for filters, joins and set operations.(转)
dplyr 0.4.0 January 9, 2015 in Uncategorized I’m very pleased to announce that dplyr 0.4.0 is now av ...
- ### 七种SQL JOINS
七种SQL JOINS 1.SELECT FROM TABLEA A LEFT JOIN TABLEB B ON A.Key=B.Key 2.SELECT FROM TABLEA A RIGHT JO ...
- MySQL Block Nested Loop and Batched Key Access Joins(块嵌套循环和批量Key访问连接)
Block Nested-Loop and Batched Key Access Joins Batched Key Access (BKA) Join算法通过index和join buffer访问j ...
- MySQL表与表之间的SQL Joins图介绍
下图很好的解释了各表之间SQL Joins之间的关系
- Lerning Entity Framework 6 ------ Joins and Left outer Joins
Joins allow developers to combine data from multiple tables into a sigle query. Let's have a look at ...
随机推荐
- (转)mysqldump: Got error: 1556: You can't use locks with log tables.
mysqldump: Got error: 1556: You can't use locks with log tables. 原文:http://blog.51cto.com/oldboy/112 ...
- PM2自动发布本地项目到服务器
(一)开发环境 本地服务器 Ubunt14.04 远程服务器Ubuntu16.04 发布软件PM 2 (二)发布介绍 本地和我们的远程服务器,必须都安装有git,pm2,以及nodejs环境.实际上配 ...
- 如何更改Android的默认虚拟机地址
第一种,虚拟机已经建立 1)找到虚拟机.ini这个文件,例如: zhai.ini 寻找方法:你可以在运行SDK Manager时看到最上面显示的虚拟机存放地址 例如显示: List of existi ...
- Kendo DataSource 概述
Kendo DataSource 概述 Kendo 的数据源支持本地数据源( JavaScript 对象数组),或者远程数据源(XML, JSON, JSONP),支持 CRUD 操作(创建,读取,更 ...
- Android里的网格空隙
在很多移动端或者web端开发中我们会遇到很多网格布局,如果我们使用线性布局来实现一些简单的网格布局就需要使用padding/margin等属性来使其对齐,代码如下: <LinearLayout ...
- Android 虚拟导航挡住应用底部解决方案(屏幕底部的三个按键)
我在华为P6测试机上测试,发现底部的三个虚拟按钮遮挡了我的应用.类似效果如:https://www.zhihu.com/question/35292413#answer-28473700 解决方案 在 ...
- To run dex in process, the Gradle daemon needs a larger heap
http://blog.csdn.net/u012995856/article/details/52595653
- Android笔记--View绘制流程源码分析(一)
Android笔记--View绘制流程源码分析 View绘制之前框架流程分析 View绘制的分析始终是离不开Activity及其内部的Window的.在Activity的源码启动流程中,一并包含 着A ...
- 微软大礼包 | 集合在线学习资源,助你秒变AI达人
编者按:人工智能的浪潮正如火如荼地袭来,未来人工智能将大有所为,人们的生活轨迹也正在技术不断向前推进的过程中逐渐改变.人工智能不是科研人员或开发人员的专属,微软希望能够将人工智能带给每个人,从开发者到 ...
- SqlServer 填充因子的说明
CREATE NONCLUSTERED INDEX IX_d_name ON department(d_name) with fillfactor=30 使用 fill factor 选项可以指定 M ...