我得到了下表,它用于根据重量范围计算电荷
Charge Weight
1 0.5
2 1
3 2
4 5我想计算一下一个重2.5的盒子的电荷,
select charge from charge_table where weight > 2.5 limit 1;或者应该使用此选项,但它会复制数据?
Charge min max
1 0 0.5
2 0.5 1
3 1 2
4 2 5然后使用以下查询
select * from charge_table where min > 2.5 and max <= 2.5;建立数据库的理想方法是什么?
发布于 2014-05-23 22:57:06
考虑第一种方法。正确的查询是:
select charge
from charge_table
where weight > 2.5
order by weight desc
limit 1;您需要在charge_table(weight, charge)上建立一个索引。索引的一个明智的用途是查找值2.5,然后找到它旁边的第一个值。
使用第二种方法,查询是正确的。它可以在min/max上使用索引。然而,由于不平等,索引的使用可能不那么有效。为了达到同等的效率,您可以:
select *
from charge_table
where min > 2.5 and max <= 2.5
limit 1;https://stackoverflow.com/questions/23839406
复制相似问题