I have a table that has records
id_queue | user_id | id_book | status
69 | 5 | 4 | 1
133 | 3 | 4 | 2
142 | 1 | 4 | 0
I want a query that will give me this result
id_queue | id_queue
69 | 142
133 | null
I’ve tried something like this
SELECT s1.`id_queue`,s2.`id_queue` FROM `second` as s1 LEFT JOIN `second` as s2 ON s2.`book_id`=4 AND s2.`status` IN (0) WHERE s1.`book_id`=4 AND s1.`status` IN (1,2,4)
but it keeps bringing me this result.
id_queue | id_queue
69 | 142
133 | 142
I think this is because I don’t have anything identical for conditions. What can I do?
The issue is that you are joining on the
book_idnot the status, so you will always have a matching row.If you want to hide the second instance of the second
id_queuethen you can use user defined variables:See SQL Fiddle with Demo
The result is: