2013-10-25 43 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