我在MySQL中进行查询,结果不按百分比排序。我如何按百分比强制下单?它按total_sold
排序,但不按百分比排序。
以下是代码:
SELECT in_disassembly.car_id,
in_disassembly.cost,
IFNULL(SUM(product_sold.product_total_price),0) AS total_sold,
concat((IFNULL(SUM(product_sold.product_total_price),0)
/in_disassembly.cost * 100)) AS percentage_sold
FROM in_disassembly
INNER JOIN product_sold ON product_sold.product_car_id = in_disassembly.car_id
WHERE in_disassembly.car_id > $start_id AND in_disassembly.car_id < $end_id
GROUP BY in_disassembly.car_id
ORDER BY percentage_sold
固定代码
SELECT in_disassembly.car_id, in_disassembly.cost, IFNULL(SUM(product_sold.product_total_price),0) AS total_sold, ROUND((IFNULL(SUM(product_sold.product_total_price),0)/in_disassembly.cost * 100),0) AS percentage_sold
FROM in_disassembly
INNER JOIN product_sold ON product_sold.product_car_id = in_disassembly.car_id
WHERE in_disassembly.car_id > $start_id AND in_disassembly.car_id < $end_id GROUP BY in_disassembly.car_id ORDER BY percentage_sold DESC
答案 0 :(得分:3)
你在那里使用CONCAT创建一个字符串,因此它将被排序为字符串。也就是说,1接着是11,接着是2。
答案 1 :(得分:0)
您在ORDER BY percentage_sold
之后缺少ASC或DESC答案 2 :(得分:0)
尝试不将百分比连接到字符串,它应该可以工作。