Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subquery inside "IN" Clause

SELECT * FROM `image_appreciations`
WHERE `image_id` IN(SELECT `id` FROM `images` WHERE `user_id` = '1')

Is my current query, it returns zero results

SELECT `id` FROM `images` WHERE `user_id` = '1'

being the subquery returns around 8 id's which two of them are found in

SELECT * FROM `image_appreciations`
WHERE `image_id` IN(77,89)

And that works fine. But all together it fails. What am i doing wrong?

like image 983
Harry Avatar asked Apr 11 '12 20:04

Harry


2 Answers

This could be done using JOIN. Below is an example using the implicit short-hand syntax.

SELECT * FROM image_appreciations a, images i
WHERE a.image_id = i.id AND i.user_id = 1
like image 122
kba Avatar answered Nov 08 '22 18:11

kba


A subquery like this "should" work, odd that's it not. Anyway, you can try using a JOIN to solve this.

SELECT * FROM `image_appreciations`
JOIN `images` ON `image_id` = `id`
WHERE `images`.`user_id` = '1'
like image 24
Rocket Hazmat Avatar answered Nov 08 '22 18:11

Rocket Hazmat