JOIN查询没有产生正确的结果

时间:2016-11-28 22:03:21

标签: php mysql sql

我有以下查询,我正在尝试加入profile_imgusers表,以匹配oder中friends表(friend_one或friend_two)中的ID获取他们的个人资料图片或用户信息。

截至目前,我没有任何错误......只是没有找到正确的结果。应该有两个结果显示与:profile_user ... 5和2的关系,这也会为他们的profile_img提供海洋和湖泊。

参数:profile_user等于1. :total_status = 2。

我不确定我的ON条款是否正在抛弃这个条款。我不确定如何u.id =friend_one同时friend_two

有谁知道为什么这不起作用?

Here is a fiddle

SELECT f.*, u.*, p.*, IFNULL(p.img, 'profile_images/default.jpg') AS img
        FROM friends f
        JOIN
            users u
            ON u.id = (f.friend_one or f.friend_two)
        LEFT JOIN
            profile_img p
            ON p.user_id = f.friend_one or f.friend_two and p.id = (select max(p2.id) from profile_img p2 where p2.user_id = p.user_id)     
        WHERE (friend_one = :profile_user or friend_two = :profile_user)
        AND status = :total_status

完整代码,显示0结果。

$friend_status = 2;
    $friend_sql = "
        SELECT f.*, u.*, p.*, IFNULL(p.img, 'profile_images/default.jpg') AS img
        FROM friends f
        JOIN
            users u
            ON u.id = (f.friend_one or f.friend_two)
        LEFT JOIN
            profile_img p
            ON p.user_id = f.friend_one or f.friend_two and p.id = (select max(p2.id) from profile_img p2 where p2.user_id = p.user_id)     
        WHERE (friend_one = :profile_user or friend_two = :profile_user)
        AND status = :total_status
    ";
    $friend_stmt = $con->prepare($friend_sql);
    $friend_stmt->execute(array(':profile_user' => $profile_user, ':total_status' => $friend_status));
    $friend_total_rows = $friend_stmt->fetchAll(PDO::FETCH_ASSOC);
    $count_total_friend = $friend_stmt->rowCount();
?>  
        <div id="friend-list-container">
            <div id="friend-list-count">Friends <span class="light-gray"><?php echo $count_total_friend; ?></span></div>
            <div id="friend-list-image-container">
<?php
    foreach ($friend_total_rows as $friend_total_row) {
        $friend_1           = $friend_total_row['friend_one'];
        $friend_2           = $friend_total_row['friend_two'];
        $friend_img         = $friend_total_row['img'];
        $friend_username    = $friend_total_row['username'];
        if($friend_1 !== $profile_user) {
            echo $friend_1;
            echo $friend_img;
            echo $friend_username;
        }
        if($friend_2 !== $profile_user) {
            echo $friend_2;
            echo $friend_img;
            echo $friend_username;
        }
    }

3 个答案:

答案 0 :(得分:1)

在我发布下面之后我意识到mysql不支持cte - 这里是没有的版本:

SELECT f.*,
       u1.*,
       u2.*,
       p1.*,
       p2.*,
       IFNULL(p1.img, 'profile_images/default.jpg') AS img1,
       IFNULL(p2.img, 'profile_images/default.jpg') AS img2
FROM friends f
LEFT JOIN users u1 ON u1.id = f.friend_one 
LEFT JOIN users u2 ON u2.id = f.friend_two
LEFT JOIN (
  SELECT user_id, max(id) as mid
  FROM profile_img
  GROUP BY user_id
) max1 ON u1.user_id = max1.user_id
LEFT JOIN (
  SELECT user_id, max(id) as mid
  FROM profile_img
  GROUP BY user_id
) max2 ON u2.user_id = max2.user_id
LEFT JOIN profile_img p1 ON p1.user_id = f.friend_one and p1.id = max1.mid
LEFT JOIN profile_img p2 ON p2.user_id = f.friend_two and p2.id = max2.mid
WHERE (friend_one = :profile_user or friend_two = :profile_user)
        AND status = :total_status
WITH maxImage AS
(
  SELECT user_id, max(id) as mid
  FROM profile_img
  GROUP BY user_id
)
SELECT f.*,
       u1.*,
       u2.*,
       p1.*,
       p2.*,
       IFNULL(p1.img, 'profile_images/default.jpg') AS img1,
       IFNULL(p2.img, 'profile_images/default.jpg') AS img2
FROM friends f
LEFT JOIN users u1 ON u1.id = f.friend_one 
LEFT JOIN users u2 ON u2.id = f.friend_two
LEFT JOIN maxImage max1 ON u1.user_id = max1.user_id
LEFT JOIN maxImage max2 ON u2.user_id = max2.user_id
LEFT JOIN profile_img p1 ON p1.user_id = f.friend_one and p1.id = max1.mid
LEFT JOIN profile_img p2 ON p2.user_id = f.friend_two and p2.id = max2.mid
WHERE (friend_one = :profile_user or friend_two = :profile_user)
        AND status = :total_status

答案 1 :(得分:0)

如果你有一个&#34; id&#34;朋友表中的列与列#34; id&#34;相同您的用户表,我认为您应该尝试这个

ON u.id = f.id

而不是

ON u.id = (f.friend_one or f.friend_two)

答案 2 :(得分:0)

基于上述SQL Fiddle中的数据。以下是我认为有帮助的查询

select 
res1.firstname as FriendOneFirstName,
res1.lastname as FriendOneLastName,
res1.img as FriendOneImage,
user1.firstname as FriendTwoFirstName,
user1.lastname as FriendTwoLastName,
pf1.img as FirendTwoProfileImage
from
(select usr.id,usr.firstname,usr.lastname,pf.img,frds.friend_two 
 from users usr 
 inner join friends frds on usr.id=frds.friend_one
 inner join profile_img pf on usr.id = pf.user_id
 ) as res1
inner join users user1 on user1.id=res1.friend_two
inner join profile_img pf1 on user1.id=pf1.user_id
order by user1.id;