编写MYSQL以下用户查询

问题描述:

我建立一个应用程序,其中用户可以跟随其他用户,并遵循。编写MYSQL以下用户查询

用户也可以看看谁另一用户以下。

现在让我们说USER1在看着谁user2的跟随,我需要找到所有用户2是继人的ID并将其与谁user1的下面。

而不是只返回所有符合该用户的ID都user1和user2(这是我在其他论坛上见过),我需要找回以下ID和用户名都user2的还有一个标志,指示跟随的人是否也跟着user1。

我知道了在PHP中使用双每个查询的循环工作,但我担心这样的代码将是昂贵的,并会好得多有一个MySQL查询优化。

相关表和列:

following_table 
    follower_id 
    followed_id 
    following: varchar -- 'true' or 'false' 

user_table 
    user_id 
    user_name 

这是我的PHP代码:

$user_id1 = '1991'; 
$myFollowingQuery = "SELECT following_table.followed_id, user_table.user_name 
        FROM following_table 
        INNER JOIN user_table ON 
        following_table.followed_id = user_table.user_id 
        WHERE following_table.following = 'true' 
        AND following_table.follower_id = '$user_id1'"; 

$user_id2 = '1985'; 
$userFollowingQuery = "SELECT following_table.followed_id, user_table.user_name 
         FROM following_table 
         INNER JOIN user_table ON 
          following_table.followed_id = user_table.user_id 
         WHERE following_table.following = 'true' 
         AND following_table.follower_id = '$user_id2'"; 

$userFollowingResult = mysql_query($userFollowingQuery) 
          or doResponse('error',"Couldn't connect to the database"); 
$myFollowingResult = mysql_query($myFollowingQuery) 
          or doResponse('error',"Couldn't connect to the database"); 

      for($i = 0; $i< mysql_num_rows($userFollowingResult);$i++){ 

$loopArray = array(followed_id => mysql_result($userFollowingResult,$i,"followed_id"), 
        followed_name => mysql_result($userFollowingResult,$i,"user_name")); 

for($j = 0; $j< mysql_num_rows($myFollowingResult);$j++){ 
    if(mysql_result($userFollowingResult,$i,"followed_id") 
    ==mysql_result($myFollowingResult,$j,"followed_id")) { 
    $loopArray['is_following'] = 'true'; 
    break; 
    } 
    if($j==mysql_num_rows($myFollowingResult)-1){ 
    $loopArray['is_following'] = 'false'; 
    break; 
    } 
} 

$resultArray[$i] = $loopArray; 
} 

echo json_encode($resultArray); 
+0

我不是肯定你的意思,是不是像我一样列出你想要的下列列表? – 2012-07-06 00:25:58

+0

你也可以粘贴表格结构('show create table TABLE_NAME')。从我猜测你可以使用MINUS,INTERSECT来解决你的问题。 – golja 2012-07-06 00:48:07

+0

对不起,是一个新手。从未使用过(show create table TABLE_NAME)。试图让它与Querious合作但没有结果。基本上following_table包括: follower_id | followed_id | date_created |下列(true或false)。 的USER_TABLE包括: USER_ID | user_name |追随者|以下|等等等。 – 2012-07-06 01:44:15

下面是一个简单的查询:

http://sqlfiddle.com/#!2/6b8d6/3

SELECT 
    user.user_id, 
    user.user_name, 
    he.follower_id AS follower_id, 
    IF(me.followed_id,1,0) AS metoo 

FROM  following AS he 

INNER JOIN user 
    ON user.user_id = he.followed_id 

LEFT JOIN following AS me 
    ON me.follower_id = 1 
    AND me.followed_id = he.followed_id 

WHERE he.follower_id = 2 
+0

很酷。无论如何,从user_table获取he.followed_name? 这是我试图得到的最终JSON: {“followed_id”:“27”,“FollowingName”:“userNameHere”,“is_following”:“true”} – 2012-07-06 04:49:15

+0

嗨,我更新了我的答案。 – biziclop 2012-07-06 04:55:58

+0

你这么摇滚!这工作完美。非常感谢你的帮助。 – 2012-07-06 05:36:36