mysql查询 - ORDER BY案例条件

时间:2015-05-25 21:02:11

标签: php mysql join

我尝试根据其中一个表字段对结果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。无论如何,我总是得到失败的消息。任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

您需要在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