我的数据:
id a b price
1 2.00 1.00 2.50
2 2.00 2.00 3.00
3 3.00 1.00 4.50
4 3.00 2.00 2.50
5 3.00 3.00 3.00
6 4.00 1.00 4.50
我的问题是 如何从a列和b列而不是id列中使用“之间”的sql获取ID 2到4的数据
2 2.00 2.00 3.00
3 3.00 1.00 4.50
4 3.00 2.00 2.50
我的代码sql(但不起作用):
sql = "SELECT * FROM data_table WHERE (( BETWEEN (a = 2.00, b = 2.00 ) AND ( a = 3.00, b = 2.00 ))";
答案 0 :(得分:3)
不确定为什么要使查询变得复杂。试试这个
Select * from data_table where id between 2 and 4
答案 1 :(得分:1)
两个列都可以使用BETWEEN
。
sql = "SELECT * FROM data_table WHERE a BETWEEN 2 AND 3 AND b BETWEEN 2 AND 4";
只需确保在两个约束之间都必须使用AND
和OR
。
答案 2 :(得分:1)
尝试一下:
SELECT * FROM data_table WHERE id BETWEEN (SELECT id FROM data_table WHERE a=2.00 AND b=2.00) AND (SELECT id FROM data_table WHERE a=3.00 AND b=2.00)
答案 3 :(得分:1)
Select * from data_table where (a > 1 and a < 5 ) or (b > 1 and b < 5)
或者您可以限制使用以下内容
Select * from data_table where (a > 1 and a < 5 ) and (b > 1 and b < 5)
答案 4 :(得分:1)
如果我正确理解了您的问题,则希望获取A和B列的值在2到4之间的行
尝试此查询
SELECT *
FROM data_table
WHERE (a BETWEEN 2 AND 4)
AND (b BETWEEN 2 AND 4)