繁体   English   中英

MySQL查询-案例条件

[英]mysql query - ORDER BY Case condition

我尝试根据表字段之一对结果ASCDESC进行排序。 这是我的查询:

  SELECT   
     tenders.id                                                                                  AS tender_id,
     tenders.title                                                                               AS tender_title,
     tenders.descr                                                                               AS tender_desc,
     tenders.init_date                                                                           AS init_date,
     tenders.init_time                                                                           AS init_time,
     tenders.end_date                                                                            AS end_date,
     tenders.end_time                                                                            AS end_time,
     tenders.city_sing                                                                           AS city_sing,
     tenders.tender_header                                                                       AS tender_header,
     tenders.tender_win                                                                          AS tender_win,
     tender_items.id                                                                             AS item_id,
     tender_items.tender_id                                                                      AS item_tender_id,
     tender_items.item_name                                                                      AS item_name,
     tender_items.item_quan                                                                      AS item_quan,
     tenders_items_bids.tender_id                                                                AS bid_tender_id,
     tenders_items_bids.id                                                                       AS bid_id,
     tenders_items_bids.user_id                                                                  AS bidder_id,
     tenders_items_bids.single_price                                                             AS single_price,
     tenders_items_bids.single_vat                                                               AS single_vat,
     tenders_items_bids.supply                                                                   AS supply,
     tenders_items_bids.timeout                                                                  AS timeout,
     tenders_items_bids.on_date                                                                  AS on_date,
     tenders_items_bids.on_time                                                                  AS on_time,
     clients.id                                                                                  AS client_id,
     clients.name                                                                                AS client_name,
     Sum((tenders_items_bids.single_vat+tenders_items_bids.single_price)*tender_items.item_quan) AS sub_total,
     Sum(tenders_items_bids.single_price*tender_items.item_quan)                                 AS sub_total_no_vat,
     cities.id                                                                                   AS ct_id,
     cities.name                                                                                 AS city_name,
     cities.logo                                                                                 AS city_logo set @orderBy = tender_win
  FROM
     tenders
  JOIN tender_items
     ON tenders.id = tender_items.tender_id
  JOIN cities
     ON cities.id = tenders.city_sing
  JOIN tenders_items_bids
     ON tenders_items_bids.tender_id = tenders.id
     AND tenders_items_bids.item_id = tender_items.id
  JOIN clients
     ON clients.id = tenders_items_bids.user_id
  WHERE
     tenders.id = $tender_id
  GROUP BY 
     tenders_items_bids.user_id
  ORDER BY
           CASE
                    WHEN tender_win = '1' THEN sub_total ASC
                    ELSE sub_total DESC
           end

最后一部分是真正重要的部分,即ORDER BY 无论如何,我总是收到失败消息。 谁能帮我这个查询?

您需要按以下order by使用两个单独的语句执行此操作:

ORDER BY (CASE WHEN tender_win = '1' THEN sub_total END) ASC,
         (CASE WHEN tender_win = '1' THEN NULL ELSE sub_total END) DESC

或者,如果sub_total是一个数字,则可以使用取反技巧:

ORDER BY (CASE WHEN tender_win = '1' THEN sub_total ELSE - sub_total END) ASC

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM