加入Group By&;MySQL中的按用例排序

加入Group By&;MySQL中的按用例排序,mysql,join,group-by,sql-order-by,case,Mysql,Join,Group By,Sql Order By,Case,为什么这个查询不起作用?这是因为订单和分组的结合吗 一个表包含广告,另一个表包含订阅,第三个表包含服务,第四个表包含服务和位置之间的多对多关系(位置是显示广告的位置) 我想要的是先对存储在位置为2的广告表中的广告进行排序,然后对那些没有定义位置的广告进行排序,然后对位置为1的广告进行排序(此顺序是通过编程生成的) 广告表: id,名称,订阅\u id 订阅表: 订阅id、服务id、日期、付费等 服务位置表: 服务id,位置id 正如您可以看到的,在这种情况下,有第四个表,但它并不重要 查询:

为什么这个查询不起作用?这是因为订单和分组的结合吗

一个表包含广告,另一个表包含订阅,第三个表包含服务,第四个表包含服务和位置之间的多对多关系(位置是显示广告的位置)

我想要的是先对存储在位置为2的广告表中的广告进行排序,然后对那些没有定义位置的广告进行排序,然后对位置为1的广告进行排序(此顺序是通过编程生成的)

广告表:
id,名称,订阅\u id

订阅表:
订阅id、服务id、日期、付费等

服务位置表:
服务id位置id

正如您可以看到的,在这种情况下,有第四个表,但它并不重要


查询:

select adverts.id, GROUP_CONCAT(service_locations.location_id) AS locations from adverts 
    left join subscriptions 
        on adverts.subscription_id = subscriptions.id
    left join service_locations 
        on service_locations.service_id = subscriptions.service_id
    group by adverts.id
    order by case service_locations.location_id 
        when 2 then 1 
        when 1 then 3 
        else 2 
    end

预期成果:

+----+-----------+
| id | locations |
+----+-----------+
|  1 | 2         |
|  3 | 1,2       |
|  2 | null      |
+----+-----------+

我实际得到的(第三行的位置为2,但它位于null之后):


使用
分组依据
时,不在
分组依据
中的所有列都应具有聚合功能。所以,我想你打算这样做:

select a.id, GROUP_CONCAT(sl.location_id) AS locations
from adverts a left join
     subscriptions s
     on a.subscription_id = s.id left join
     service_locations sl
     on sl.service_id = s.service_id
group by a.id
order by max(case sl.location_id 
               when 2 then 1 
               when 1 then 3 
               else 2 
             end);
我不确定
max()
是否是您真正需要的,但您确实需要一个聚合函数。这将具体产生问题中的输出:

order by (case min(sl.location_id)
               when 2 then 1 
               when 1 then 2
               else 3
           end);

我找到了一个解决方案,order by必须在group by之前执行,而group by不是默认行为,更多关于该行为的信息请参见:)(必须使用子查询)

所以,查询应该如下所示

select *, GROUP_CONCAT(location_id) as locations from (
    select adverts.id AS id, service_locations.location_id AS location_id from adverts 
    left join subscriptions 
        on adverts.subscription_id = subscriptions.id
    left join service_locations 
        on service_locations.service_id = subscriptions.service_id
    order by case service_locations.location_id 
        when 2 then 1 
        when 1 then 3 
        else 2 
    end
    ) as table 
    group by table.id
    order by case table.location_id 
        when 2 then 1 
        when 1 then 3 
        else 2 
    end
select *, GROUP_CONCAT(location_id) as locations from (
    select adverts.id AS id, service_locations.location_id AS location_id from adverts 
    left join subscriptions 
        on adverts.subscription_id = subscriptions.id
    left join service_locations 
        on service_locations.service_id = subscriptions.service_id
    order by case service_locations.location_id 
        when 2 then 1 
        when 1 then 3 
        else 2 
    end
    ) as table 
    group by table.id
    order by case table.location_id 
        when 2 then 1 
        when 1 then 3 
        else 2 
    end