简体   繁体   English

MySql 中的多个内部联接不起作用

[英]Multiple inner joins in MySql not working

I have 3 tables :我有 3 张桌子:

useractivitylog用户活动日志

id   userid   activity_type  activity_id   date

which will store all user activities.它将存储所有用户活动。

requestfriend请求好友

opid  userid  friendid  requesttime  message  source  requesttype  friendemail  status

which will store friends details.这将存储朋友的详细信息。 userid and friendid can be the user's id. useridfriendid可以是用户的id。 If user A has send friend's request to user B, then userid will contain the id of A and friendid will be the id of B. So anyway B is the friend of A and A is the friend of B.如果用户 A 向用户 B 发送了好友请求,那么userid将包含 A 的 id,并且friendid将是 B 的 id。所以无论如何 B 是 A 的朋友,A 是 B 的朋友。

auth_user_profiles auth_user_profiles

id  user_id  first_name  last_name  profileimage

which stores user profile information.它存储用户配置文件信息。

What I need is to select all my friends activities from useractivitylog table.我需要的是从useractivitylog表中选择我所有的朋友活动。 I tried with the following query.我尝试了以下查询。 But it didnt worked.但它没有奏效。

    echo "select ua.*,rf.opid,aup.user_id,aup.first_name,aup.last_name,
        aup.profileimage from useractivitylog as ua INNER JOIN requestfriend as rf 
   INNER JOIN auth_user_profiles as aup on
   aup.user_id= IF (rf.userid='$userid',rf.friendid,rf.userid) 
   WHERE (rf.userid=$userid or rf.friendid=$userid)
   and rf.status='2' and ua.userid!=$userid";

Here, $userid is my user id.这里, $userid是我的用户 ID。 Actually what I want is to retrieve the profile information and the activities of users who are friends of mine.其实我想要的是检索个人资料信息和作为​​我朋友的用户的活动。 But know, it is returning all rows in useractivitylog table even if the userid is not in my friends list.但是要知道,即使用户 ID不在我的朋友列表中,它也会返回useractivitylog表中的所有行。

Can anyone help me to find an appropriate query for this.任何人都可以帮我找到一个合适的查询。 Thanks in advance.提前致谢。

This should work as you explained you need to get the activities of users who are friends with you, you first need to join useractivitylog table to requestfriend where friendid is in useractivitylog ie ON(rf.friendid =ua.userid) then you need to join auth_user_profiles table where userid from useractivitylog table is equal,ie ON(ua.userid=aup.user_id) and then use you where filter on rf.userid is equal to the provided user id ,i didn't get the point of using OR rf.friendid = $userid comparison when you asked for the friends activity then why comparing friend's id to the given user id这应该工作,你解释你需要得到谁是朋友,你的用户的活动,你首先需要加入useractivitylogrequestfriend其中friendiduseractivitylogON(rf.friendid =ua.userid)那么你需要加入auth_user_profiles表,其中useriduseractivitylog表是相等的,即ON(ua.userid=aup.user_id)然后使用你在哪里上的过滤器rf.userid等于所提供的用户ID,我没有得到使用的点OR rf.friendid = $userid比较当您询问朋友活动时,为什么将朋友的 id 与给定的用户 id 进行比较

SELECT 
  ua.*,
  rf.opid,
  aup.user_id AS aup_user_id,
  aup.first_name,
  aup.last_name,
  aup.profileimage 
FROM
  useractivitylog AS ua 
  INNER JOIN requestfriend AS rf ON(rf.friendid =ua.userid)
  INNER JOIN auth_user_profiles AS aup ON(ua.userid=aup.user_id)

WHERE 
  rf.userid = $userid      
  AND rf.status = '2' 

Using a sub select使用子选择

SELECT 
  ua.*,  
  aup.user_id AS aup_user_id,
  aup.first_name,
  aup.last_name,
  aup.profileimage 
FROM
  useractivitylog AS ua 
  INNER JOIN auth_user_profiles AS aup ON(ua.userid=aup.user_id)
WHERE 
ua.userid IN (
SELECT friendid  FROM  requestfriend WHERE userid =$userid AND `status` = '2' 
) 

You don't join properly.你没有正确加入。 The only condition for a useractivitylog being linked to the other tables is that its user ID is not $userid.将用户活动日志链接到其他表的唯一条件是其用户 ID 不是 $userid。 That is true for most records in the table, so you almost get a cartesian product (cross join).对于表中的大多数记录都是如此,因此您几乎可以得到笛卡尔积(交叉连接)。

BTW: You don't join auth_user_profiles properly either, because you compare with the string '$userid' instead of comparing with $userid.顺便说一句:您也没有正确加入 auth_user_profiles,因为您与字符串 '$userid' 进行比较,而不是与 $userid 进行比较。

Let's re-write the statement such that we retrieve the friends first and join the other tables with these:让我们重新编写语句,以便我们首先检索朋友并使用这些加入其他表:

select 
  ua.*,
  friend.opid,
  aup.user_id,
  aup.first_name,
  aup.last_name,
  aup.profileimage 
from 
(
  select distinct
    case when userid = $userid then friendid else userid end as id,
    opid
  from requestfriend
  where $userid in (userid, friendid) and status = '2'
) as friend
inner join useractivitylog as ua on ua.userid = friend.id
inner join auth_user_profiles as aup on aup.user_id = friend.id;

Try as below query尝试如下查询

select * from useractivitylog a,requestfriend b,auth_user_profiles c 
   WHERE a.userid = b.userid and b.userid = c.userid;

You need to add a field to JOIN your first table on:您需要添加一个字段来加入您的第一个表:

SELECT ua.*, rf.opid, aup.user_id, aup.first_name, aup.last_name, aup.profileimage 
  FROM useractivitylog as ua 
       INNER JOIN requestfriend as rf on fieldname = fieldname
       INNER JOIN auth_user_profiles as aup on aup.user_id = IF (rf.userid='$userid',rf.friendid,rf.userid) 
 WHERE (rf.userid=$userid or rf.friendid=$userid)
   AND rf.status='2' 
   AND ua.userid!=$userid

You could go from the other side to your data... Start with selecting all your friends and if you have them, get their data:你可以从另一边转到你的数据......从选择你所有的朋友开始,如果你有他们,获取他们的数据:

SELECT 
  ua.*,
  rf.opid,
  aup.user_id,
  aup.first_name,
  aup.last_name,
  aup.profileimage
FROM
  requestfriend AS rf
JOIN 
  useractivitylog AS ua ON (ua.userid = rf.friendid)
JOIN 
  auth_user_profiles AS uap ON (uap.userid = rf.friendid)
WHERE rf.userid = $userid AND rf.status = '2'

The rest with the vice-versa connection in your requestfriend table should be no problem. requestfriend 表中与反之亦然连接的其余部分应该没问题。

Since the friendrequests should be not as much as the activities, this might lead to better performance in case you have loads of data...由于好友请求不应与活动一样多,因此如果您有大量数据,这可能会带来更好的性能......

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

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