使用3个表检索单行

时间:2013-10-25 07:05:02

标签: mysql case

我有3个表,即person,person2,person3。每个表包含两个字段名称和phno。 如果我给出一个特定的phno,查询必须在每个表中显示该数字的存在

我试过这样的事情:

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”。

4 个答案:

答案 0 :(得分: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';

答案 1 :(得分: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

答案 2 :(得分: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

或者如果您通过名称或其他方式加入它,请输入a.name = b.name或a.id = b.id或a.something = b.something 如果你想看到所有数字只是删除

答案 3 :(得分: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