简体   繁体   中英

How do I correct query a friends table for user and friend data without distinct, or union

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:

SELECT `photos`.`id` 
   FROM `photos` 
        LEFT JOIN `friends` ON (`friends`.`friend_id` = `photos`.`user_id`) 
   WHERE `friends`.`user_id` = 1
union all 
SELECT `photos`.`id` FROM `photos` WHERE `photos`.`user_id` = 1

If you do not want to use UNION then

SELECT `photos`.`id` 
   FROM `photos` where `photos`.`user_id`=1
                   or `photos`.`user_id` 
                       in (select `friends`.`friend_id` from `friends`
                                             where `friends`.`user_id` = 1)

I think union is the best approach for this problem.

or you can try to use a subquery with 'OR'

SELECT photos.id FROM photos WHERE 
(photos.user_id = 1) or 
(photos.user_id in (select friends.friend_id from friends where friends.user_id=1))

For what you have shown, you don't need the JOIN at all. Since the photos table already contains the user ids, the friends table isn't needed for anything in what you have shown. Just query photos directly to get the photos of particular users.

Maybe you really want something different, such as only returning a photo if someone actually is a friend. In that case, you need inner join rather than left join, and you can do it like this:

SELECT  `photos`.`id` 
FROM  `photos` 
INNER JOIN  `friends` ON
  `friends`.`friend_id` =  `photos`.`user_id`  AND
  `friends`.`user_id` =1
LIMIT 0 , 30

If you want to return the photos of friend with ID 1, and also the photos of user ID 2 whether that person is a friend or not, do something like this:

SELECT  `photos`.`id` 
FROM  `photos` 
LEFT JOIN  `friends` ON
  `friends`.`friend_id` =  `photos`.`user_id`  AND
  `friends`.`user_id` = 1
WHERE
  `friends`.`friend_id` IS NOT NULL OR
  `photos`.`user_id` = 2
LIMIT 0 , 30

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM