sql 語句系列 多表之鏈二 八百章之第四章

2022-03-02 10:58:34 字數 590 閱讀 3204

比如說查詢每個員工的部門,且檢視部門的所有員工。

這裡考慮一種情況就是可能有部門沒有員工,同樣有些員工還沒有分配部門。

解析使用 full outer join.

select d.deptno,d.dname,e.ename

from dept d full outer join emp e

on (d.deptno=e.deptno)

因為null值是不能比較的,那麼可以把null值轉換為0;

select ename,comm

from emp

where coalesce(comm,0)<(select comm from emp where ename='ward')

coalesce返回乙個傳入引數的非空值。

如果我們使用coalesce,像這樣:

select ename,comm

from emp

where comm<(select comm from emp where ename='ward')

那麼就會自動排除null項。

複習之SQL語句(二) 常用多表查詢語句使用

有些時候資料在不同的表中,這個時候我們就需要用到多表聯查 sql join 用於把來自兩個或多個表的行結合起來。table order表 create table table order orderid int 11 not null auto increment comment 訂單編號 user...

SQL基礎語句 多表操作

合併多個表join on join inner join 根據某列合併兩個表,只合併兩個表中在該列的共有值,不共有的值忽略 select from orders join customers on orders.customer id customers.customer id left join ...

sql語句的多表查詢方式

例如 按照 department id 查詢 employees 員工表 和 departments 部門表 的資訊。方式一 通用型 select from where select e.last name,e.department id,d.department name from employe...