我有3個表,即person,person2,person3。每個表格都包含兩個字段name和phno。 如果我給一個特定的PHNO查詢在每個表使用3個表檢索單行
我想是這樣的,以顯示這個數字的存在:
select a.name as Name, a.phno,
case when a.phno then 'Y' else 'N' end as Phone_Number1,
case when b.phno then 'Y' else 'N' end as Phone_Number2,
case when c.phno then 'Y' else 'N' end as Phone_Number3
from person as a, person2 as b, person3 as c
where a.phno = '123456' and b.phno = '123456' and c.phno = '123456';
這個查詢只能在所有表中包含的值對於特定PHNO ..
我需要出去放像
phno Phone_Number1 Phone_Number2 Phone_Number3
123456 Y Y Y
,如果它存在於所有的表
phno Phone_Number1 Phone_Number2 Phone_Number3
123456 N Y Y
如果它不存在,則 'N' 應在該特定表dispayed ..
這是行不通的。如果任何表中缺少該行,則聯接不會返回任何內容,因爲它是交叉產品。 – Barmar
嘗試沒有'where'? – jerdiggity
現在,您將獲得數十億行,因爲您正在生成所有3個整個表的交叉產品。 – Barmar