gpt4 book ai didi

mysql:按相同字段值对周围记录进行分组

转载 作者:行者123 更新时间:2023-11-29 12:52:40 24 4
gpt4 key购买 nike

我有一个具有以下结构和数据的表格:

id | type | title
--------------------------
1 | 1 | test 1
2 | 1 | test 2
3 | 2 | test 3
4 | 2 | test 4
5 | 1 | test 5

我需要将具有相同 type 字段值的相邻行分组。

所以结果应该是这样的:

type |
------
1 |
2 |
1 |

提前致谢。

最佳答案

这应该可以解决问题..使用用户定义的变量。

SELECT 
type
FROM(
SELECT
type,
if(@a = type, @b, @b := @b + 1) as grouping_col,
@a := type
FROM testing
JOIN (select @a := 1, @b := 0) as temp
) as t
GROUP BY grouping_col;

SQL FIDDLE一起玩

关于mysql:按相同字段值对周围记录进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24495864/

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