Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where A=1 AND A=2 returns 0 row problem - (Short Mysql question)

Tags:

sql

mysql

I have the following table (id, Tag) with the following values

(1, 17)
(1, 31)
(2, 17)
(3, 31)

When I query the following

"SELECT id FROM table WHERE 1 AND Tag=17 AND Tag=31"

I expected it to return (id)

(1)

But it doesn't. (returns 0 row)

What's wrong here?

like image 520
user706087 Avatar asked Dec 13 '22 13:12

user706087


2 Answers

For any particular row the tag can't be both 17 and 31. You need

SELECT id 
FROM   table 
WHERE  Tag in (17, 31) 
GROUP  BY id 
HAVING COUNT(DISTINCT Tag) = 2  
like image 124
Martin Smith Avatar answered Mar 02 '23 20:03

Martin Smith


Machines are logic and do what you asked them to do... your expectations are wrong in this case.

Tag 17 AND tag 31 conditions according to boolean math should be satisfied simultaneous to produce results.

like image 37
Elzo Valugi Avatar answered Mar 02 '23 20:03

Elzo Valugi