gpt4 book ai didi

mysql - 强制 group by 保持行的原始顺序

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

我有这样的查询结果:

24
1
24
11
11
11
13
24
24
11
24
11
24
11
11
13
24
24
11
11
13
21
21
11
11
11
11
11
7
11
11

我需要对这个结果进行分组,但是如果我简单地按这个 id 进行分组,mysql也会按如下方式排序:

1
2
3
4

我想要的是:

24
1
11
13
21

。。.

这可能吗?

完整的查询是这个,基本上它根据给定的坐标计算内部有商家的最近路线:

select * from (select dr.*  from drivers_route dr inner join businesses_drivers_route bdr on dr.id_drivers_route = bdr.fk_id_drivers_route inner join  (select * from (select b.idbusiness, 
b.trading_as,
111.1111 *
DEGREES(ACOS(COS(RADIANS(53.33841959999999)) * COS(RADIANS(b.latitude))
* COS(RADIANS(-6.2876401 - b.longitude))
+ SIN(RADIANS(53.33841959999999))
* SIN(RADIANS(b.latitude)))) AS distance_in_km from business b inner join business_category bc on bc.fk_idbusiness = b.idbusiness where b.status_reg = 'A' and bc.status_reg = 'A' and bc.fk_idcategory=44) as b_dist order by b_dist.distance_in_km) as b_ord on b_ord.idbusiness = bdr.fk_id_bus_destination) as r_ord;

最佳答案

您可以通过在查询中添加计数器来做到这一点,如下所示:

设置@c=0;SELECT my_field,(@c := @c +1) as c FROM my_table group by my_field order by c;

但正如 Strawberry 所说,最好有一个唯一的主键,这样您就可以使用它。

关于mysql - 强制 group by 保持行的原始顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36009243/

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