I have a table called friends and a table photos. I’m trying to query friends photos OR the users who has the friends photos. I think I need to alias an inner join on the friends table but not sure
Friends Table
user_id int
friend_id int
[other attribute field]
Photos Table
id int
user_id int
[other attribute field]
For example if I want to query friends photos i’d run
SELECT `photos`.`id` FROM `photos` LEFT JOIN `friends` ON (`friends`.`friend_id` = `photos`.`user_id`) WHERE `friends`.`user_id` = 1
Or if I wanted to query just the users table id do
SELECT `photos`.`id` FROM `photos` WHERE `photos`.`user_id` = 1
The problem is when I try to get both in the same result set like
SELECT `photos`.`id`
FROM `photos`
LEFT JOIN `friends` ON ( `friends`.`friend_id` = `photos`.`user_id` )
WHERE `friends`.`user_id` =1
OR `photos`.`user_id` =1
LIMIT 0 , 30
Which obviously gets the wrong results because of the left join I get a result for every friend I have
I’m thinking I need to inner join an aliased table for the OR clause or something but can’t figure it out.
What if just use UNION for two your first queries:
If you do not want to use UNION then