2014-03-03 64 views
2

我想用不同的值PHP/SQL:選擇2列3次不同的值

SELECT id FROM ads WHERE onoff='1' AND again='0' 
SELECT id FROM ads WHERE onoff='2' AND again='1' 
SELECT id FROM ads WHERE onoff='3' AND again='1' 

來選擇2列兩次是有辦法做到這一切在一個SQL?

+0

能不能請您嘗試重組你的問題,它只是沒有任何意義。 – idmean

回答

6
SELECT id 
FROM ads 
WHERE (onoff = '1' AND again = '0') 
    OR (onoff = '2' AND again = '1') 
    OR (onoff = '3' AND again = '1') 
1

更好的執行比使用OR:

SELECT id FROM ads WHERE onoff='1' AND again='0' 
UNION ALL 
SELECT id FROM ads WHERE onoff='2' AND again='1' 
UNION ALL 
SELECT id FROM ads WHERE onoff='3' AND again='1' 

OR

SELECT ID FROM ADS WHERE onoff='1' AND again='0' 
         OR (onoff IN('2','3') AND again = '1')