gpt4 book ai didi

mysql - 检查表 MySQL 中是否存在值

转载 作者:行者123 更新时间:2023-11-29 03:08:59 27 4
gpt4 key购买 nike

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

我有这个问题:

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。

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

最佳答案

刚刚在 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;

关于mysql - 检查表 MySQL 中是否存在值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11092545/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com