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 ...
随机推荐
- E. Mike and Foam 容斥原理
http://codeforces.com/problemset/problem/548/E 这题是询问id,如果这个id不在,就插入这个id,然后求a[id1] , a[id2]互质的对数. 询问 ...
- ssas 为绑定指定的大小太小,导致一个或多个列值被截断
错误信息:ssas 为绑定指定的大小太小,导致一个或多个列值被截断 如果更改了某个维度或是事实表的字段长度,在处理CUBE时提示此错误,我们要做以下更新: 1.刷新数据源视图. 2.打开多维数据集,查 ...
- Mybatis基础配置及增删查改操作
一.简介 平时我们都用JDBC访问数据库,除了需要自己写SQL之外,还必须操作Connection, Statement, ResultSet 这些其实只是手段的辅助类. 不仅如此,访问不同的表,还会 ...
- JS判断android ios系统 PC端和移动端
最近公司上线移动端,需要根据不同的系统跳转到不同的产品页面,百度后发现这一段代码很好用,不但可以判断当前是什么系统,还能知道当前浏览器是什么内核,移动端PC端都已测试无问题! var browser ...
- $.ajax同步/异步(async:false/true)
虽然说ajax用来执行异步请求的比较多,但有时还是存在需要同步执行的情况的. 比如:我需要通过ajax取执行请求以返回一个值,这个值在ajax后面是需要使用到的,这时就不能用异步请求了.这时候就需要使 ...
- Redis哨兵原理详解
一.概述 Redis哨兵(以下称哨兵)是为Redis提供一个高可靠解决方案,对一定程序上的错误,可以不需要人工干预自行解决. 哨兵功能还有监视.事件通知.配置功能.以下是哨兵的功能列表: 监控:不间断 ...
- codevs 4093 EZ的间谍网络
时间限制: 10 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 由于外国间谍的大量渗入,学校安全正处于高度的危机之中.YJY决定挺身而作出反抗 ...
- Python-OpenCV——Morphological Transformations(形态学转换)
目标 这一节 我们将学习不同的形态学操作,如腐蚀.膨胀.开.闭...... 我们将看到不同的函数,如:cv2.erode().cv2.dilate().cv2.morphology() 理论 形态变换 ...
- JS实现单向链表、双向链表、循环链表
https://cloud.tencent.com/developer/article/1114246 链表存储有序的元素的集合,但是和数组不同的是,链表中的元素在内存中的存储并不是连续的.每一个链表 ...
- Ubuntu编译Android源码过程中的空间不足解决方法
Android源码一般几十G,就拿Android5.0来说,下载下来大概也有44G左右,和编译产生的文件以及Ubuntu系统占用的空间加起来,源码双倍的空间都不够有.编译源码前能分配足够的空间再好不过 ...