gpt4 book ai didi

MySQL如何分组为行的子组

转载 作者:太空宇宙 更新时间:2023-11-03 11:39:15 24 4
gpt4 key购买 nike

我有这个数据库:(注意 id 是唯一的)

enter image description here

我想得到这个:

enter image description here

实现此目标的高效 SQL 查询是什么?

最佳答案

SELECT 
DISTINCT
foo2.group_id AS group_id,
if(foo1.group_id = foo2.group_id, foo1.id, NULL) AS id,
if(foo1.group_id = foo2.group_id, foo1.some_attr, NULL) AS some_attr
FROM mytable AS foo1
JOIN
(SELECT DISTINCT group_id FROM mytable) as foo2
ORDER BY group_id, id

关于MySQL如何分组为行的子组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43286667/

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