2013-10-25 46 views
0

我有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 ..

回答

0

我想嘗試添加比較以每種情況下/時:

select a.name as Name, a.phno, 
case when a.phno = '123456' then 'Y' else 'N'    end as Phone_Number1, 
case when b.phno = '123456' then 'Y' else 'N' end as Phone_Number2, 
case when c.phno = '123456' then 'Y' else 'N' end as Phone_Number3 
from person as a, person2 as b, person3 as c 
where a.phno = '123456' or b.phno = '123456' or c.phno = '123456'; 
+0

這是行不通的。如果任何表中缺少該行,則聯接不會返回任何內容,因爲它是交叉產品。 – Barmar

+0

嘗試沒有'where'? – jerdiggity

+0

現在,您將獲得數十億行,因爲您正在生成所有3個整個表的交叉產品。 – Barmar

0
SELECT MAX(Phone_Number1) Phone_Number1, MAX(Phone_Number2) Phone_Number2, MAX(Phone_Number3) Phone_Number3 
FROM (SELECT "Y" Phone_Number1, "N" Phone_Number2, "N" Phone_Number3 
     FROM person 
     WHERE phno = '123456' 
     UNION 
     SELECT "N" Phone_Number1, "Y" Phone_Number2, "N" Phone_Number3 
     FROM person2 
     WHERE phno = '123456' 
     UNION 
     SELECT "N" Phone_Number1, "N" Phone_Number2, "Y" Phone_Number3 
     FROM person3 
     WHERE phno = '123456' 
     UNION 
     SELECT "N", "N", "N" -- In case they're not in any table 
    ) u 
0

試試這個

SELECT * FROM (
    SELECT IFNULL(IFNULL(a.name, b.name), c.name) AS Name, IFNULL(IFNULL(a.phno,b.phno),c.phno) AS phone, 
    CASE WHEN a.phno IS NULL THEN 'N' ELSE 'Y' END AS Phone_Number1, 
    CASE WHEN b.phno IS NULL THEN 'N' ELSE 'Y' END AS Phone_Number2, 
    CASE WHEN c.phno IS NULL THEN 'N' ELSE 'Y' END AS Phone_Number3 
    FROM `person` AS a 
    RIGHT OUTER JOIN person2 AS b ON a.phno = b.phno 
    RIGHT OUTER JOIN person3 AS c ON a.phno = c.phno 
    UNION ALL 
    SELECT IFNULL(IFNULL(a.name, b.name), c.name) AS Name, IFNULL(IFNULL(a.phno, b.phno), c.phno) AS phone, 
    CASE WHEN a.phno IS NULL THEN 'N' ELSE 'Y' END AS Phone_Number1, 
    CASE WHEN b.phno IS NULL THEN 'N' ELSE 'Y' END AS Phone_Number2, 
    CASE WHEN c.phno IS NULL THEN 'N' ELSE 'Y' END AS Phone_Number3 
    FROM `person` AS a 
    LEFT OUTER JOIN person2 AS b ON a.phno = b.phno 
    LEFT OUTER JOIN person3 AS c ON a.phno = c.phno 
) k 
WHERE k.phone = '123456' 
GROUP BY k.phone 

,或者如果您通過名稱或東西埃爾加入它se把a.name = b.name或a.id = b.id或a.something = b.something 如果你想看到所有的數字只是刪除其中

0

很好的問題,謝謝發佈。

請嘗試以下方法選擇查詢:

select a.name as Name, a.phno, 
case when a.phno = '123456' then 'Y' else 'N' end as Phone_Number1, 
case when b.phno = '123456' then 'Y' else 'N' end as Phone_Number2, 
case when c.phno = '123456' then 'Y' else 'N' end as Phone_Number3 
from person as a, person2 as b, person3 as c 

或者

select a.name as Name, a.phno, 
case a.phno when '123456' then 'Y' else 'N' end as Phone_Number1, 
case b.phno when '123456' then 'Y' else 'N' end as Phone_Number2, 
case c.phno when '123456' then 'Y' else 'N' end as Phone_Number3 
from person as a, person2 as b, person3 as c