mysql选择具有最低值的记录

时间:2013-07-06 21:09:05

标签: mysql division

我有一张表

TOTAL_PRICE 单位 单元名

我想选择total_price / units最低的记录。

如何在查询中执行除法然后选择具有最低值的记录?

(我还希望按单位名分组 - 即如果一个是盎司,一个是升)

1 个答案:

答案 0 :(得分:1)

您只需在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
相关问题