2016-02-25 63 views
0

可以說我有一個名爲「測試」與這些列的表和值詢問int值

ID Apple Pear Pineapple Orange 
1 2  3 4   4 
2 1  12 1   0 

現在我想打一個SELECT語句返回所有行任何列中的值爲4。

我可以這樣做,但我有很多列檢查,所以我需要找到其他方式來做到這一點。

SELECT * FROM Test 
WHERE test.apple = 4 or test.apple = 4 or test.pineapple = 4 or test.pineapple = 4 

回答

0

一種方法是使用in

select t.* 
from test t 
where 4 in (t.apple, t.pear, t.pineapple, t.orange); 

你仍然需要列出列,但他們只是走在in列表。

1

試試這個:

SELECT * 
FROM Test 
WHERE 4 IN (Apple, Pear, Pineapple, Orange)