2010-01-15 12 views
3

我有两列存储值(数字),我如何选择我的给定数字在两列中的值之间的位置?如何在mysql中的列之间搜索

`id | col1 | col2` 
    `1 | 20 | 50` 
    `2 | 200 | 400` 
    `3 | 500 | 650` 

如果我有25个值,我怎么能选择记录,其中的25值是他们在这种情况下将排在1

回答

10
select * from mytable where 25 between col1 and col2; 
+0

一定要使用EXPLAIN来检查优化器将使用这一个适当的索引;理想情况下,它应该能够扫描col1或col2 – MarkR 2010-01-15 08:02:49

4

你可以试试:

如果你想在搜索中包含col1和COL2:

select * from table where YOUR_NUM >= col1 and YOUR_NUM <= col2; 

如果不是:

select * from table where YOUR_NUM > col1 and YOUR_NUM < col2;