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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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)

Method 3

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x