2013-06-24 112 views
1

我有一個查詢下面。此查詢應該返回多行,但只返回兩行,因爲有些行沒有狀態表中的連接行。任何人都可以幫助我修復此查詢,以便計算所有行,即使狀態表中沒有連接行。Informix外連接

SELECT count(h.h1_id) 
FROM h1 h, owner o, ent e, status s 
WHERE o.owner_id = h.owner_id AND e.enterprise_id = h.enterprise_id AND 
h.herd_id=s.o_id AND s.o_type='H' AND h.code = 'QWE' 
AND s.group_code!='123' AND s.status_code!='ABC' 

謝謝!

+0

搜索'Informix的外Join',第二個結果是[官方尋找教程](http://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/ com.ibm.sqlt.doc/sqltmst104.htm),它描述了informix特定的(如果需要的話,如果你是一個真正的古老版本)和ANSI標準的外部連接方式。 –

回答

1
SELECT count(h.h1_id) 
FROM h1 h 
INNER JOIN owner o 
ON o.owner_id = h.owner_id 
INNER JOIN ent e 
ON e.enterprise_id = h.enterprise_id 
LEFT OUTER JOIN status s 
ON h.herd_id=s.o_id 
where h.code = 'QWE' 
AND ((s.o_type='H' AND s.group_code!='123' AND s.status_code!='ABC') OR (s.oid is null))