lgt
lgt

Reputation: 1034

mysql syntax on not equal many values

I'm trying to get the right syntax for the following case?

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

Upvotes: 33

Views: 120618

Answers (3)

Samuel
Samuel

Reputation: 17171

The <> operator compares a single left and right argument to see if they are not equal. In your case you have one left hand argument that needs to be checked (I assume) to see if the ID is none of the values on the right. Therefore you should use ID NOT IN (5616,1095,1357,271,2784,902)

Upvotes: 5

Himanshu
Himanshu

Reputation: 32622

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

Upvotes: 4

podiluska
podiluska

Reputation: 51514

Instead of <> , you can use 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 

Upvotes: 92

Related Questions