简体   繁体   English

在查询中联接多个MySQL表

[英]JOIN multiple MySQL tables in query

I'm not sure if this is even possible, or if my JOIN-fu just isn't strong enough(it's pretty wimpy to tell you the truth). 我不确定这是否可能,或者我的JOIN-fu是否不够强壮(实话实在太虚弱了)。 I have 3 tables which are tied together with a UID. 我有3个与UID捆绑在一起的表。 I'm trying to get information from a result from all of them into one query, except I'm having trouble with making sure the result is what I want. 我正在尝试将所有信息的结果信息汇总到一个查询中,但我无法确保结果是否是我想要的。

users 使用者

 ========= USERS ========== | uid | nickname | -------------------------- | testusr1 | Test User 1 | | testusr2 | Test User 2 | | testusr3 | Test User 3 | | testusr4 | Test User 4 | ============= GALLERY =========== | id | uid | ext | profile | --------------------------------- | 1 | testusr1 | png | 1 | | 2 | testusr2 | jpg | 1 | | 3 | testusr3 | png | 1 | | 4 | testusr4 | png | 1 | | 5 | testusr4 | jpg | 0 | ============= FRIENDS ============= | sender | reciever | status | ----------------------------------- | testusr1 | testusr3 | 0 | | testusr2 | testusr3 | 1 | | testusr2 | testusr1 | 1 | | testusr3 | testusr4 | 1 | 

What I'm trying to do is get all of a user's friends. 我正在尝试做的是获得用户的所有朋友。 Friends are in the friends table where the status = 1. The uid can be either the sender or the reciever. 朋友位于状态为1的朋友表中。uid可以是发送者也可以是接收者。 In the table above, testusr3's friends are: testusr2 and testusr4 在上表中,testusr3的朋友是:testusr2和testusr4

From here I want to snag the nickname from users, and the id from gallery WHERE profile = 1 AND uid = (that friend's ID). 在这里,我想获取用户的昵称,以及来自画廊WHERE个人资料= 1 AND uid =(该朋友的ID)的ID。

So far, my query looks like: 到目前为止,我的查询看起来像:

$query = "SELECT u.uid AS USERID, g.id, g.ext, f.sender, f.reciever 
FROM friends f
LEFT JOIN gallery g ON g.uid = f.sender AND g.profile = 1 
LEFT JOIN users u ON u.uid = f.sender 
WHERE f.status = 1 
AND (f.sender = '$sentuid' OR f.reciever = '$sentuid')";

But, it labels all of the results as f.sender...and I'm pretty sure the g.profile = 1 isn't working. 但是,它将所有结果都标记为f.sender ...,而且我很确定g.profile = 1无法正常工作。 It does grab the friends accurately though. 它确实准确地抓住了朋友。 Any help would be greatly appreciated. 任何帮助将不胜感激。

Untested Solution 未经测试的解决方案

Best place to start is to get the matching records in a single column, with UNION . 最好的开始是使用UNION在单列中获取匹配的记录。 Then you have all the UIDs you need, in one place. 然后,将所需的所有UID放在一个位置。

SELECT f.uid, u.nickname, g.id
FROM
(
    (SELECT reciever as uid FROM friends where status=1 and sender='$sentuid')
    UNION 
    (SELECT sender as uid FROM friends where status=1 and reciever='$sentuid')
) f
LEFT JOIN gallery g ON f.uid = g.uid and profile=1
LEFT Join users u ON f.uid = u.uid

Side notes: 旁注:

  • Generally a bad idea to use char for an ID field. 通常,将char用作ID字段是一个坏主意。
  • For performance reasons, you may be better off actually using more storage space, and doubling up on the 'friends' records. 出于性能方面的考虑,您最好实际使用更多的存储空间,并将“朋友”记录增加一倍。 ie: two entries for each friendship. 即:每个友谊两个条目。

Seems to me that you're really close, but a SQLfiddle would help. 在我看来,您真的很接近,但是SQLfiddle会有所帮助。 I believe you are right, g.profile = 1 is not working because you have no table reference for it, might as well take it out. 我相信您是对的,因为您没有表引用,所以g.profile = 1无法正常工作,不妨将其删除。 But because of the join, you should be able to select it. 但是由于有了连接,您应该可以选择它。

$query = "SELECT u.uid AS USERID, g.id, g.ext, g.profile, f.sender, f.reciever 
FROM friends f
LEFT JOIN gallery g ON g.uid = f.sender
RIGHT JOIN users u ON u.uid = f.sender 
WHERE f.status = 1 
AND (f.sender = '$sentuid' OR f.reciever = '$sentuid')";

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

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