gpt4 book ai didi

MySQL 将行与空条目进行比较

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

我看过很多关于自连接的例子,但它们似乎没有涵盖某些字段不在某些行中的情况。

例如,我有一个数据库:

testId, testItem, testResult

和行:

 1,test1,1
1,test2,0
1,test3,1
2,test1,0
2,test4,1
2,test5,1

我想要输出:

testItem,a.testId,b.testId,a.testResult,b.testResult
test1,1,2,1,0
test2,1,NULL,0,NULL
test3,1,NULL,1,NULL
test4,NULL,2,NULL,1
test5,NULL,2,NULL,1

本质上,我想比较来自两个不同 testId(1 和 2)的每个 testItem (test1->test5) 并比较它们的 testResult 值,将可能不具有相同测试项目的 testId 考虑在内。

最佳答案

鉴于您的确切要求,您可以尝试这样做:

select testItem
, max(case when testID = 1 then testID else null end) as testID1
, max(case when testID = 2 then testID else null end) as testID2
, max(case when testID = 1 then testResult else null end) as testResult1
, max(case when testID = 2 then testResult else null end) as testResult2
from mytable
where testID in (1,2)
group by testItem

这对您的数据做出了很多假设,因此请持保留态度。

关于MySQL 将行与空条目进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14551249/

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