0

我有 3 张桌子,即 person、person2、person3。每个表都包含两个字段 name 和 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

4 回答 4

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
于 2013-10-25T08:37:59.373 回答
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
于 2013-10-25T07:14:46.353 回答
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 如果您想查看所有数字,只需删除 where

于 2013-10-25T07:35:41.737 回答
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';
于 2013-10-25T07:10:14.177 回答