2

有没有办法检查表的列中是否存在多个值,即使这些值并不都在同一行中?

我有这个查询:

select (select country from storage where country = 'Germany' limit 1) as country,
       (select city from storage where city = 'New York City' limit 1) as city,
       (select pet from storage where pet = 'Dog' limit 1) as pet,
       (select profession from storage where profession='owehs9843' limit 1) as profession

这个查询做我想要的。如果该值存在于列中,则返回搜索到的值,否则返回 null。

一种解决方案是执行 if 语句并在找到时返回 1,否则返回 0。

但是,我不确定这是检查每列而不是每行值的正确方法。我希望该值存在于某列中,但每列的键值行不需要关联。

4

1 回答 1

1

刚刚在 MySql WorkBench(来自 Windows)上进行了测试,以下查询运行良好,当找到值时返回 1,如果没有,则返回 cero(对于每一列)。

select if(sum(country = 'Germany') = 0, 0, 1) as country,
       if(sum(city = 'New York City') = 0, 0, 1) as city,
       if(sum(pet = 'Dog') = 0, 0, 1) as pet,
       if(sum(profession = 'owehs9843') = 0, 0, 1) as profession
from storage;
于 2012-06-19T01:48:05.377 回答