根据mysql中另一个表中的数据构建Yes / No列

时间:2015-11-20 15:00:08

标签: mysql select view

所以我有一堆表,每个表都有一些关于用户的数据。

table 1: first_name, last_name, email
table 2: info_1, info_2, info_3

我正在尝试构建一个SELECT查询,它将用于构建一个VIEW,它看起来像这样

SELECT first_name, last_name, email, has_info_1, has_info_2, has_info_3 FROM tables1, 2 WHERE some condition.

但是实际上没有抓住info_1,2,3数据,我只想检查表2中是否存在该数据,并在has_info_1,2,3中说出Y或N.

我希望这是有道理的。

1 个答案:

答案 0 :(得分:0)

这样的事情:

SELECT
  Table1.First_Name,
  Table1.Last_Name,
  Table1.Email,
  CASE WHEN Table2.Info_1 IS NULL THEN 'No' ELSE 'Yes' END AS Has_Info_1,
  CASE WHEN Table2.Info_2 IS NULL THEN 'No' ELSE 'Yes' END AS Has_Info_2,
  CASE WHEN Table2.Info_3 IS NULL THEN 'No' ELSE 'Yes' END AS Has_Info_3
FROM Table1
LEFT JOIN Table2 ON Table1.Whatever = Table2.Whatever