Yusuf
Yusuf

Reputation: 53

When using WHERE NOT IN, It returns one of WHERE IN

Here is the sql code:

SELECT s_id FROM questions WHERE s_id NOT IN (:ids)

IDs are 1,2 and there is only 2 line in the table. But that selects the line 2, anyway. Where is the problem that I make??

Upvotes: 0

Views: 50

Answers (1)

deceze
deceze

Reputation: 522081

SELECT s_id FROM questions WHERE s_id NOT IN (:ids)

This has a placeholder for exactly one value, :ids. If you're binding the value like:

$stmt->bindValue(':ids', '1,2');

Well, guess what, you've just bound one value "1,2". This is the same as:

SELECT s_id FROM questions WHERE s_id NOT IN ('1,2')

Which is not the same as

SELECT s_id FROM questions WHERE s_id NOT IN (1, 2)

If you want to bind more than one value, you need more than once placeholder. Placeholders stand in for values, they're not like copy and pasted SQL snippets.

Upvotes: 1

Related Questions