如何选择我的桌子范围

时间:2015-06-12 18:04:16

标签: mysql sql select range

我有这样一张桌子: (注意每个单词的序列号)

// tablename
+---------+----------+
|  word   |  number  |
----------------------
|  jack   |     1    |
|  jack   |     2    |
|  jack   |     3    |
|  ali    |     1    |
|  ali    |     2    |
|  ali    |     3    |
|  ali    |     4    |
|  ali    |     5    |
|  peter  |     1    |
|  peter  |     2    |
|  peter  |     3    |
|  peter  |     4    |
|  raj    |     1    |
|  raj    |     2    |
+---------+----------+

现在我需要一个查询来选择一个范围。我的范围是(例如)

// Range Start
select * from tablename where word='ali' and number='2';

// Range End
select * from tablename where word='peter' and number='3';

实际上我想要下面的结果:

+---------+----------+
|  word   |  number  |
----------------------
|  ali    |     2    |
|  ali    |     3    |
|  ali    |     4    |
|  ali    |     5    |
|  peter  |     1    |
|  peter  |     2    |
|  peter  |     3    |
+---------+----------+

我该怎么做?

6 个答案:

答案 0 :(得分:1)

我必须离开,但如果你的数据看起来像ID列:

+---------+----------+----+
|  word   |  number  | ID |
---------------------------
|  jack   |     1    | 1  |
|  jack   |     2    | 2  |
|  jack   |     3    | 3  |
|  ali    |     1    | 4  |
|  ali    |     2    | 5  |
|  ali    |     3    | 6  |
|  ali    |     4    | 7  |
|  ali    |     5    | 8  |
|  peter  |     1    | 9  |
|  peter  |     2    | 10 |
|  peter  |     3    | 11 |
|  peter  |     4    | 12 |
|  raj    |     1    | 13 |
|  raj    |     2    | 14 |
+---------+----------+----+

然后你可以这样做(这只是一种方式,最有可能是更优化的方式)

select *
from table
where id >= (select id from table where word='ali' and number='2')
 and id <= (select id from table where word='peter' and number='3')

答案 1 :(得分:1)

如果你有一个Id列,这是一个简单的方法来做你需要的

SELECT
  word, number 
FROM 
  Test 
WHERE 
  id BETWEEN
    (SELECT id FROM Test WHERE word = 'ali' AND number = '2') AND
    (SELECT id FROM Test WHERE word = 'peter' AND number = '3');

Here你在SQLFiddle中有一个有效的例子

希望这有帮助

答案 2 :(得分:0)

试试这个:

select * from table 
where (word = 'ali' and number >='2') or (word = 'peter' and number <= '3')
order by word, number

答案 3 :(得分:0)

select * from tablename
where (word = 'ali' and number between '2' and '5') OR
      (word = 'peter' and number between '1' and'3')
order by word, number

答案 4 :(得分:0)

假设您希望首先按word订购数据,然后按number订购,则可以执行以下操作:

select * from table
    where strcmp(concat(word, lpad(number, 5, '0')), concat('ali', lpad(2, 5, '0'))) >= 0
    and strcmp(concat(word, lpad(number, 5, '0')), concat('peter', lpad(3, 5, '0'))) <= 0
    order by word asc, number asc;

这将获取两个字段,并将一个ASCII字符串nameNNNNN与之进行比较。这将独立于记录ID的排序。如果您将wordnumber设置为复合索引,则可以根据该订单进行排序,而不使用字符串比较和连接。

答案 5 :(得分:0)

    select * from tablename where 
(word = 'ali' and number >= 2) or 
(word = 'peter' and number <= 3);

应该产生您正在寻找的结果,但是如果您有一个在表中作为索引唯一的主键,您可以使用类似的东西查询表;

select * from tablename where index between x and y;

这显然表明你知道你需要的索引值,尽管你可以使用像这样的嵌套条件来获取它们;

    select * from tablename where index between 
(select index from tablename where name = 'ali' and number =2) and 
(select index from tablename where name=peter and number = 5);
希望有所帮助。