mysql语法不等于很多值

时间:2012-08-24 14:34:21

标签: mysql

我正在尝试为以下情况获取正确的语法?

SELECT * 
FROM wp_posts AS p 
WHERE post_type = 'post' 
AND post_status = 'publish' 
AND ID <> 5616,1095,1357,271,2784,902
ORDER BY post_title DESC

3 个答案:

答案 0 :(得分:52)

您可以使用<>

而不是NOT IN (5616,1095...)
SELECT * 
FROM wp_posts AS p 
WHERE post_type = 'post' 
AND post_status = 'publish'
AND ID NOT IN (5616,1095,1357,271,2784,902)
ORDER BY post_title DESC 

答案 1 :(得分:2)

SELECT * FROM wp_posts AS p WHERE post_type = 'post' 
AND post_status = 'publish' AND 
ID NOT IN (5616,1095,1357,271,2784,902) ORDER BY post_title DESC

答案 2 :(得分:2)

<>运算符比较单个左右参数,看它们是否相等。在你的情况下,你有一个需要检查的左手参数(我假设),看看ID是不是右边的值。因此,您应该使用ID NOT IN (5616,1095,1357,271,2784,902)