如何“排序”比列更多?

时间:2014-02-06 13:23:57

标签: enums sql-order-by

这是我的疑问:

SELECT count(oi.id) imgCnt, o.*, 
IF(pricet=2,c.currency_value*o.attributes_36*o.price,
c.currency_value*o.price) AS pprice, od.title, oi.image, 
MIN(oi.id), ( c.currency_value * o.price ) AS fprice, 
ag.agent_name, DATE_FORMAT( o.date_added, '%d-%m-%Y') as dadded 
FROM i_offers_12 o 
LEFT JOIN i_agents ag ON o.agents_id = ag.id 
LEFT JOIN i_currencies c ON o.currencies_id = c.id 
LEFT JOIN i_offers_details od ON ( o.id = od.offers_id AND od.languages_id = 1 ) 
LEFT JOIN i_offers_images oi ON ( oi.offers_id = o.id AND oi.o_id = '12' ) 
WHERE ( o.offer_status='active' OR o.offer_status='sold') 
AND actions_id = '1' 
AND c.id = o.currencies_id 
AND o.counties_id = '2' 
AND o.cities_id = '3' 
GROUP BY o.id
ORDER BY dadded
DESC

我想在dadded(类型为date)和offer_status(类型为enum)之后进行排序。 我想首先显示所有offer_status ='有效'并按dadded排序的元素,之后所有元素都有offer_status ='已售出'并排序由爸爸。我怎样才能做到这一点? thx

3 个答案:

答案 0 :(得分:1)

您要排序的字段必须是select语句的一部分:

  SELECT count(oi.id) imgCnt, o.*, 
    IF(pricet=2,c.currency_value*o.attributes_36*o.price,
    c.currency_value*o.price) AS pprice, od.title, oi.image, 
    MIN(oi.id), ( c.currency_value * o.price ) AS fprice, 
    ag.agent_name, DATE_FORMAT( o.date_added, '%d-%m-%Y') as dadded ,
    offer_status
    FROM i_offers_12 o 
    LEFT JOIN i_agents ag ON o.agents_id = ag.id 
    LEFT JOIN i_currencies c ON o.currencies_id = c.id 
    LEFT JOIN i_offers_details od ON ( o.id = od.offers_id AND od.languages_id = 1 ) 
    LEFT JOIN i_offers_images oi ON ( oi.offers_id = o.id AND oi.o_id = '12' ) 
    WHERE ( o.offer_status='active' OR o.offer_status='sold') 
    AND actions_id = '1' 
    AND c.id = o.currencies_id 
    AND o.counties_id = '2' 
    AND o.cities_id = '3' 
    AND o.offer_status='active' 
    GROUP BY o.id
    ORDER BY offer_status, dadded
    DESC

请注意,您通常应按所有非汇总字段分组(o。*,     IF(pricet = 2,c.currency_value * o.attributes_36 * o.price,     c.currency_value * o.price)AS pprice,od.title,oi.image,(c.currency_value * o.price)AS fprice,     ag.agent_name,DATE_FORMAT(o.date_added,'%d-%m-%Y')为爸爸,     offer_status)

MySQL没有强制执行,但像Oracle这样的其他数据库也是如此。

答案 1 :(得分:0)

我相信你可以用

编写SQL
ORDER BY offer_status, dadded

答案 2 :(得分:0)

您可以使用逗号,例如

ORDER BY supplier_city DESC, supplier_state ASC;