klakekent

如何对查询内容进行二次排序

  •  
  •   klakekent · Jun 3, 2018 · 6676 views
    This topic created in 2907 days ago, the information mentioned may be changed or developed.
    现在遇到这样一个需求

    我的索引里面保存了从各个网站爬回来对产品以及价格信息,基本信息是这样

    product_id, source_id,price,discount

    所以我通过 collapse 对 product_id 进行折叠分组,并且是通过 price 来排序的,所以得到对结果就是每个 product 的最低价,但是我现在还想对这个返回对结果再次排序,比如通过 discount 来排序,应该怎么做呢?
    mysql 大概是这样

    select * from (select * from product group by product_id order by price asc ) t order by discount desc


    用 aggregation 目前没有思路
    5 replies    2018-06-04 14:20:35 +08:00
    WhyAreYouSoSad
        1
    WhyAreYouSoSad  
       Jun 3, 2018 via iPhone
    重新定义一下这个问题,是不是按 a 字段排序,当值相等的时候,再用 b 排序。
    klakekent
        2
    klakekent  
    OP
       Jun 3, 2018
    @WhyAreYouSoSad 不是

    这个需求是首先选出所有产品在各个网站的最低价,所以我用了 collapse 通过 order by price desc 来取顶层数据

    取出来的结果集可以在按照折扣百分比 折扣数量等条件再来排序一次,这个二次排序我就不知道怎么弄了
    WhyAreYouSoSad
        3
    WhyAreYouSoSad  
       Jun 3, 2018
    @klakekent
    了解下权值排序,然后想下你自己说的二次排序的定义是什么,什么条件下进行。这算是需求的一部分。
    9066v
        4
    9066v  
       Jun 4, 2018
    select * from product group by product_id order by price asc, 用这样的语句是无法获取到正确的数据的,group by 查询出来的数据是按照主键排序的。
    klakekent
        5
    klakekent  
    OP
       Jun 4, 2018
    @WhyAreYouSoSad 其实就是我现在可以通过 collapse 来按照最低价格分组 拿到每个 product 的最低价的那条数据详细信息, 我还想再基于这个结果集,再来一次排序
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5715 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 03:20 · PVG 11:20 · LAX 20:20 · JFK 23:20
    ♥ Do have faith in what you're doing.