假设我有一个包含以下数据的表格:
| id | t0 | t1 | t2 |
______________________
| 1 | 4 | 5 | 6 |
| 2 | 3 | 5 | 2 |
| 3 | 6 | 4 | 5 |
| 4 | 4 | 5 | 9 |
| 5 | 14 | 5 | 49 |
我想检索包含4,5,6的所有行(无论表中数字的位置如何),因此第1行和第1行将选择第3行。如何使用SQL查询?
该表包含数千条记录。
答案 0 :(得分:7)
你可以这样做:
select *
from table
where 4 in (t0, t1, t2)
and 5 in (t0, t1, t2)
and 6 in (t0, t1, t2)
答案 1 :(得分:0)
你总是可以用“硬”的方式来做:
select *
from tbl
where (t0 = 4 AND t1 = 5 AND t2 = 6)
or (t0 = 5 AND t1 = 6 AND t2 = 4)
or (t0 = 6 AND t1 = 4 AND t2 = 5)
答案 2 :(得分:0)
另一种“硬”方式:
DECLARE @table TABLE (id int, t0 int, t1 int, t2 int)
INSERT INTO @table(id, t0, t1, t2) VALUES(1, 4, 5, 6)
INSERT INTO @table(id, t0, t1, t2) VALUES(2, 3, 3, 2)
INSERT INTO @table(id, t0, t1, t2) VALUES(3, 6, 4, 5)
INSERT INTO @table(id, t0, t1, t2) VALUES(4, 4, 5, 5)
SELECT *
FROM @table
WHERE (t0+t1+t2) = 15
AND t0 BETWEEN 4 AND 6
AND t1 BETWEEN 4 AND 6
AND t2 BETWEEN 4 AND 6
AND t0 <> t1