不错SQL join讲解
1:关于表的别名说明:
select a.employee_id ,b.department_id from employees a, departments b
where employees.department_id = b.department_id;
以上sql如果运行就会报错,如果改为:
select a.employee_id ,b.department_id from employees a, departments b
where A.department_id = b.department_id;
就运行正常了。
2:关于运算的优先级:
3:select 语法
4:连接查询
- The columns must be the same data type.
- Don’t use ON clause in a natural join.
select department_id , department_name, location_id ,city
from departments natural join locations;
这两个表就链接起来了。以上是SQL标准写法。
下面利用oracle syntax 来写
select d.department_id ,d.department_name,
d.location_id , l.city from departments d ,locations l
where d.location_id = l.location_id;
这两个结果相同,都是查询出来27行结果:
SQL> desc departments;
名称 是否为空? 类型
----------------------------------------- -------- ----------------------------
DEPARTMENT_ID NOT NULL NUMBER(4)
DEPARTMENT_NAME NOT NULL VARCHAR2(30)
MANAGER_ID NUMBER(6)
LOCATION_ID NUMBER(4)
SQL> desc locations;
名称 是否为空? 类型
----------------------------------------- -------- ----------------------------
LOCATION_ID NOT NULL NUMBER(4)
STREET_ADDRESS VARCHAR2(40)
POSTAL_CODE VARCHAR2(12)
CITY NOT NULL VARCHAR2(30)
STATE_PROVINCE VARCHAR2(25)
COUNTRY_ID CHAR(2)
以上是两个表的结构可以看出。
select employee_id ,last_name, department_id
from employees natural join departments;
2:
select employee_id ,last_name, a.department_id
from employees a,departments b where a.department_id = b.department_id
and a.manager_id = b.manager_id;
select employee_id ,last_name,location_id,department_id
from employees join departments using(department_id)
;
以上语句等同于 等同于natural join 或者 oracle 本身的句法。
select employee_id ,last_name, a.department_id
from employees a,departments b where a.department_id = b.department_id
and a.manager_id = b.manager_id;
运用on 连接 进行条件过滤 ,可以用 and 过滤 也可以用 where过滤,如下图:

3:内连接:
在SQL1999标准中规定,两个连接返回仅仅相匹配的行叫做内连接
4:外连接
左外连接(LETT OUT JOIN):
The result of a left outer join (or simply left join) for table A and B always contains all records of the "left" table (A), even if the join-condition does not find any matching record in the "right" table (B). This means that if the ON
clause matches 0 (zero) records in B (for a given record in A), the join will still return a row in the result (for that record)—but with NULL in each column from B. A left outer join returns all the values from an inner join plus all values in the left table that do not match to the right table.
右外连接(RIGHT OUT JOIN):
A right outer join (or right join) closely resembles a left outer join, except with the treatment of the tables reversed. Every row from the "right" table (B) will appear in the joined table at least once. If no matching row from the "left" table (A) exists, NULL will appear in columns from A for those records that have no match in B.
A right outer join returns all the values from the right table and matched values from the left table (NULL in case of no matching join predicate). For example, this allows us to find each employee and his or her department, but still show departments that have no employees.
5:全连接(FULL JOIN)
Conceptually, a full outer join combines the effect of applying both left and right outer joins. Where records in the FULL OUTER JOINed tables do not match, the result set will have NULL values for every column of the table that lacks a matching row. For those records that do match, a single row will be produced in the result set (containing fields populated from both tables).
5:笛卡尔集
CROSS JOIN returns the Cartesian product of rows from tables in the join. In other words, it will produce rows which combine each row from the first table with each row from the second table
笛卡尔集返回两个表中乘积的所有数据,
比如 A 表有 8行数据, B 表有 9行数据
笛卡尔集返回 72行数据,
一般情况下不用笛卡尔集,如果需要制造大批量,海量数据的时候可以用多个表进行笛卡尔集,制造测试数据是笛卡尔集的一个最大的好处。
Example of an explicit cross join: 显式笛卡尔集
SELECT *
FROM employee CROSS JOIN department;
Example of an implicit cross join: 隐式笛卡尔集
SELECT *
FROM employee, department;