简体   繁体   English

如何在没有distinct或union的情况下更正查询朋友表中的用户和朋友数据

[英]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表上为内联接设置别名但不确定

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 或者,如果我只想查询用户表id

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. 我想我需要为OR子句内部加入一个别名表或者其他东西,但是无法解决它。

What if just use UNION for two your first queries: 如果你的第一个查询只使用UNION两个怎么办:

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 如果你不想使用UNION那么

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' 或者您可以尝试使用带有'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. 对于您所展示的内容,您根本不需要JOIN Since the photos table already contains the user ids, the friends table isn't needed for anything in what you have shown. 由于photos表已经包含用户ID,因此您所显示的内容中不需要所有friends表。 Just query photos directly to get the photos of particular users. 只需直接查询photos即可获取特定用户的照片。

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: 在这种情况下,您需要inner join而不是左联接,您可以这样做:

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: 如果您想要返回ID为1的朋友的照片,以及用户ID 2的照片,无论该人是否是朋友,请执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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