SQL fiddle link for sample data
基本上我的疑问是,如果我们在 LEFT OUTER JOIN 的 ON 子句中指定条件,或者我们在 WHERE 中指定条件,是否相同带有空检查的子句?
表架构:
Create table App(ID number , STATUS varchar2(10));
Create table App_Child(child_id number ,
STATUS varchar2(10),
ID number );
查询1
SELECT a.ID AS appID, a.STATUS AS appSTATUS,
b.child_id AS acOWNID,b.STATUS AS acSTATUS, b.id AS acID
FROM App a LEFT OUTER JOIN App_Child b ON (a.id=b.id AND b.STATUS <> 'disabled') WHERE a.ID = ?;
查询2
SELECT a.ID AS appID, a.STATUS AS appSTATUS,
b.child_id AS acOWNID,b.STATUS AS acSTATUS, b.id AS acID
FROM App a LEFT OUTER JOIN App_Child b ON (a.id=b.id) WHERE a.ID = ? AND (b.STATUS IS NULL OR b.STATUS<>'disabled');
答案 0 :(得分:2)
结果和可读性都不一样。
on
子句中写入条件时,您将包含App中的所有行。where
子句中写入条件时,您会从结果中过滤行:如果您的App
行与b_STATUS ='已禁用'的App_Child相关,则会过滤
这是一个示例:
INSERT INTO App VALUES(1,'active');
INSERT INTO App_Child VALUES(3,'disabled',1);
SELECT a.ID AS appID, a.STATUS AS appSTATUS,
b.child_id AS acOWNID,b.STATUS AS acSTATUS, b.id AS acID
FROM App a LEFT OUTER JOIN App_Child b
ON (a.id=b.id AND b.STATUS <> 'disabled')
WHERE a.ID = 1;
--- has results ---
SELECT a.ID AS appID, a.STATUS AS appSTATUS,
b.child_id AS acOWNID,b.STATUS AS acSTATUS, b.id AS acID
FROM App a LEFT OUTER JOIN App_Child b ON (a.id=b.id)
WHERE a.ID = 1 AND (b.STATUS IS NULL OR b.STATUS<>'disabled');
-- don't has results --