gpt4 book ai didi

mysql - 如何在mysql中将数量从最高到最低排序?

转载 作者:行者123 更新时间:2023-11-29 09:35:39 24 4
gpt4 key购买 nike

我想将药品数量从高到低排序。问题是最低数量先于最高数量。如何将数量从最高到最低排序?这是我的查询

  SELECT tbl_transaction_details.supplier_medicine_id, tbl_supplier_medicine.medicine_name
AS
MedicineName, tbl_category.description
AS
Category, tbl_unit_category.unit
AS
Unit_Category, SUM(tbl_transaction_details.quantity)
AS
Quantity
FROM
tbl_transaction_details
INNER JOIN
tbl_supplier_medicine ON tbl_transaction_details.supplier_medicine_id = tbl_supplier_medicine.supplier_medicine_id
INNER JOIN
tbl_category ON tbl_category.category_id = tbl_supplier_medicine.category_id
INNER JOIN
tbl_unit_category ON tbl_unit_category.unit_category_id = tbl_supplier_medicine.unit_category_id
INNER JOIN
tbl_transaction ON tbl_transaction.transaction_id = tbl_transaction_details.transaction_id
INNER JOIN
tbl_barangay ON tbl_barangay.barangay_id = tbl_transaction.barangay_id
WHERE
tbl_barangay.barangay_id = 1
AND
(tbl_transaction.transaction_date between DATE_FORMAT(NOW() ,'%M-%d-01') AND NOW()) GROUP BY tbl_transaction_details.supplier_medicine_id ASC
ORDER BY MAX('Quantity'), COUNT(tbl_transaction_details.supplier_medicine_id) DESC

预期结果

MedicineName   Category Unit_Category QuantityParacetamol    Tablet   500mg         171Atenolol       Tablet   100mg         24Metoprolol     Tablet   100mg         8Ibuprofenv     Tablet   200mg         3

最佳答案

为什么不在 ORDER BY 子句中使用 DESC ?只需在末尾添加:

... 按数量描述排序

关于mysql - 如何在mysql中将数量从最高到最低排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57660685/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com