我有一张 table
总价 单位 单位名称
我想选择total_price/units最低的记录。
如何在查询中执行除法,然后选择具有最低值的记录?
(我还想按单位名称分组 - 即,如果一个是盎司,一个是升)
最佳答案
您只需在 order by
子句中使用除法即可:
select *
from t
order by total_price / units
limit 1;
为了安全起见 - 如果 units
为 0 - 你可以这样做:
select *
from t
where units <> 0
order by total_price / units
limit 1;
如果您希望通过 unit_name
获得此值,那么您需要使用 min()
:
select unit_name, min(total_price / units)
from t
group by unit_name
关于mysql选择具有最低值的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17507173/