gpt4 book ai didi

mysql - 迭代两列并比较它们

转载 作者:行者123 更新时间:2023-11-29 11:33:23 25 4
gpt4 key购买 nike

我有两列这样的:

结果集 1:select sPersonID from tPersonData where x < 50

结果集 2:select sPersonID from tPersonData where x > 50

我想迭代第 1 列,并检查每个 sPeronID 是否在第 2 列中至少出现 1 次。

列的示例数据:

Result-Set 1:
00/510
00/650
00/644
00/690

Result-Set 2:
00/510
00/640
00/644

预期结果应该是 TRUE/FALSE

最佳答案

使用左连接自连接 tPersonData,并从左侧按 sPersonID 进行分组,并从右侧计算相应记录的数量:

select t1.sPersonID, if(count(t2.sPersonID)=0,'false','true') as result
from tPersonData t1
left join tPersonData t2 on t1.sPersonID=t2.sPersonID and t2.x>50
where t1.x<50
group by t1.sPersonID

关于mysql - 迭代两列并比较它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37023091/

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