在mysql中使用内连接和concat()操作进行查询

时间:2013-01-31 06:39:06

标签: php mysql inner-join

我有两张桌子:

sk_accounts      //details of user
  • acnt_user_id
  • acnt_fname //名字
  • acnt_lname
  • acnt_profile_picture
  • acnt_member_class
  • 等........

     sk_following   //table containing details of users who are following others
    
  • ID

  • flwing_follower_id //被其他关注者关注的用户的ID
  • flwing_following_user_id
  • following_date

    我想根据以下Mysql代码显示关注者的详细信息。不幸的是,虽然有3行,但它返回零行。我的查询是这样的:

    $query_following = "SELECT sk_following.flwing_following_user_id, 
    sk_accounts.acnt_fname,
    sk_accounts.acnt_lname,
    sk_accounts.acnt_member_class,
    sk_accounts.acnt_profile_picture
    FROM sk_following 
    INNER JOIN sk_accounts 
    WHERE sk_following.flwing_follower_id='$id' AND        sk_accounts.acnt_user_id=sk_following.flwing_following_user_id AND CONCAT(sk_accounts.acnt_fname,' ',sk_accounts.acnt_lname)='$name'";
    $result_following = mysql_query($query_following);
    $count_following = mysql_num_rows($result_following);
    echo $count_following;
    

注意:$ id和$ name包含值 请帮助我。谢谢。

2 个答案:

答案 0 :(得分:2)

试试这个,

"SELECT sk_following.flwing_following_user_id,
sk_accounts.acnt_fname,
sk_accounts.acnt_lname,
sk_accounts.acnt_member_class,
sk_accounts.acnt_profile_picture 
FROM sk_following
LEFT JOIN sk_accounts ON sk_accounts.acnt_user_id=sk_following.flwing_following_user_id
WHERE sk_following.flwing_follower_id='$id'
AND CONCAT(sk_accounts.acnt_fname,' ',sk_accounts.acnt_lname)='$name'";

这可能对你有所帮助。

答案 1 :(得分:1)

如果没有看到样本数据和所需的输出,很难完全理解,但是你的JOIN应该在flwing_follower_id而不是flwing_following_user_id吗?

SELECT sk_following.flwing_following_user_id,
    sk_accounts.acnt_fname,
    sk_accounts.acnt_lname,
    sk_accounts.acnt_member_class,
    sk_accounts.acnt_profile_picture 
FROM sk_following 
    INNER JOIN sk_accounts ON sk_accounts.acnt_user_id=sk_following.flwing_follower_id 
WHERE sk_following.flwing_follower_id='$id' 
    AND CONCAT(sk_accounts.acnt_fname,' ',sk_accounts.acnt_lname)='$name'
祝你好运。