PHP / SQL:使用不同的值选择2列3次

时间:2014-03-03 19:52:01

标签: php mysql sql sql-server

我想用不同的值

两次选择2列
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'

有没有办法在一个sql中完成所有这些?

2 个答案:

答案 0 :(得分:6)

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

答案 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')