SQL通过desc选择顺序

时间:2014-04-24 22:22:45

标签: php mysql sql

这是我的sql查询,我只想从销售表中按降序排序 需要帮助提前致谢。

e.g。

SELECT salesMan.salesmanID, salesMan.name, SUM(sale.sellingPrice)
FROM salesMan 
INNER JOIN sale ON salesMan.salesmanID = sale.salesManID
GROUP BY salesmanID, name;

3 个答案:

答案 0 :(得分:3)

然后添加:

order by SUM(sale.sellingPrice) desc

答案 1 :(得分:1)

将别名添加到SUM列,并使用它来订购结果集。

Select salesMan.salesmanID,salesMan.name, SUM(sale.sellingPrice) as sellingPriceSum
FROM salesMan INNER JOIN
     sale
     ON salesMan.salesmanID = sale.salesManID
GROUP BY salesmanID,name
ORDER BY sellingPriceSum desc;

答案 2 :(得分:1)

这应该有效:

Select salesMan.salesmanID, salesMan.name, SUM(sale.sellingPrice) AS totalSale
FROM salesMan INNER JOIN
     sale
     ON salesMan.salesmanID = sale.salesManID
GROUP BY salesmanID, name
ORDER BY totalSale DESC;

您可以像这样简化查询:

SELECT sm.salesmanID, sm.name, SUM(s.sellingPrice) AS totalSale
FROM salesMan sm 
INNER JOIN sale s ON sm.salesmanID = s.salesManID
GROUP BY sm.salesmanID, sm.name
ORDER BY totalSale DESC;