MySQL:在行列表中使用给定值查找正确的行

时间:2012-04-26 18:17:31

标签: mysql

我有一张名为折扣的表

Field        Type 
id           int(11) NOT NULL
resort_id    int(11) NOT NULL
count_from   int(11) NULL
count_to     int(11) NULL
discount     varchar(255) NULL

我的行如下:

 
  id |product_id |count_from |count_to |discount
    1|8          |0          |30       |22
    2|8          |31         |60       |12
  

当我得到27作为输入时,我应该检索第一行(id:1)。假设我的输入是33,我应该检索2行(id:2)。

如何编写mysql查询?

的问候,
帕布

1 个答案:

答案 0 :(得分:4)

SELECT *
FROM discounts
WHERE 27 BETWEEN count_from AND count_to

SELECT *
FROM discounts
WHERE 33 BETWEEN count_from AND count_to