我有一个这样的SQLITE表:
id key value
244574 16 999
244574 18 999
244574 54 174
214808 16 662
214808 17 808
214808 33 1
214808 60 2
214809 16 902
214809 17 1103
214809 33 1
214809 60 2
218965 19 808
218965 21 662
218965 33 1
218965 60 8
218966 19 1103
218966 21 902
218966 33 1
218966 60 8
小提琴:单击此处
现在,我尝试获取一个ID列表,其中键不是60或键是60且value = 8。
select * from items_attributes where (key != 60) OR (key = 60 AND value = 8);
我经常得到那些键不是60的行。但是我只想得到三行:
id key value
244574 [16;18;54] [999;999;174]
218965 [19;21;33;60] [808;662;1;8]
218966 [19;21;33;60] [1103;902;1;8]
因为我是SQL的初学者,所以我可以想到使用concat的解决方案,但是我不知道tbh。也许有人可以在这里帮助我。
提前致谢!
最好的问候,安德里亚斯
怎么样:
select
id,
'[' || group_concat(key, ';') || ']',
'[' || group_concat(value, ';') || ']'
from items_attributes
group by id
having
-- no rows with key 60 for this id
count(case when key = 60 then 1 end) = 0
or
-- 1 or more rows with key 60 & value 8 for this id
count(case when key = 60 and value = 8 then 1 end) > 0
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句