限制在我的SQL查询中不起作用

时间:2013-09-06 09:52:21

标签: php mysql

我在php中创建了一个“你可能知道的人”脚本。这个脚本在php朋友系统中向我展示了我朋友的朋友。我有一个名为用户(user_id,姓名,电子邮件,个人资料)的表,其中包含有关用户的信息。另一个名为朋友(friend_id,user_one,user_two)的表格,其中包含作为朋友的用户的ID。我的代码如下:

<?php

//----- get friends of friends -------


$friends_of_friends = mysql_query(" SELECT u.*
FROM (SELECT DISTINCT user_one as user_id
        FROM friends
        WHERE user_two IN (SELECT user_one as user_id
                  FROM friends
                  WHERE user_two = '$session_user_id'
                  UNION DISTINCT
                  SELECT user_two
                  FROM friends
                  WHERE user_one = '$session_user_id')
        UNION DISTINCT
    SELECT DISTINCT user_two
        FROM friends
        WHERE user_one IN (SELECT user_one as user_id
              FROM friends
                  WHERE user_two = '$session_user_id'
                  UNION DISTINCT
              SELECT user_two
              FROM friends
              WHERE user_one = '$session_user_id')
     ) f
 JOIN users u
 ON u.user_id = f.user_id  ");


  while ($run_friends= mysql_fetch_assoc($friends_of_friends)) {

  $friend_friend_id = $run_friends['user_id'];  



// ---- friends of my friends that are not my friends --------------


$check_friend_query = mysql_query("  SELECT friends_id from friends WHERE  (user_one='$session_user_id' AND user_two='$friend_friend_id') OR (user_one='$friend_friend_id' AND user_two='$session_user_id')   ");

if (mysql_num_rows($check_friend_query) != 1){ 

    $not_friends = mysql_query("SELECT `user_id`, `name`, `surname`, `email`, `profile` FROM `users` WHERE (`user_id`='$friend_friend_id' AND `user_id`!='$session_user_id')  "); 



    while ($run_not_friends= mysql_fetch_assoc($not_friends)) {
      $i=1;
    if ($i <= 3)
    {                                                           
    $not_friend_id = $run_not_friends['user_id'];

                  echo $not_friend_id;

     $i++;
     }
    }

  }//end if

 }//end while

?>

一切正常。我想要的只是选择和展示3个人,这是我朋友的朋友(因为我可能有一个列表,许多人是我朋友的朋友)。我在我的sql查询中使用LIMIT,你可以看到,但它不起作用。任何想法该怎么做?

2 个答案:

答案 0 :(得分:3)

试试这段代码。将打印3个朋友ID。我没试过。

$i=1;
while ($run_friends= mysql_fetch_assoc($friends_of_friends)) {

     $friend_friend_id = $run_friends['user_id'];  
     // ---- friends of my friends that are not my friends --------------

     if ($i <= 3)
     { 
          $check_friend_query = mysql_query("  SELECT friends_id from friends WHERE  (user_one='$session_user_id' AND user_two='$friend_friend_id') OR (user_one='$friend_friend_id' AND user_two='$session_user_id')   ");

          if (mysql_num_rows($check_friend_query) != 1){ 
              $not_friends = mysql_query("SELECT `user_id`, `name`, `surname`, `email`, `profile` FROM `users` WHERE (`user_id`='$friend_friend_id' AND `user_id`!='$session_user_id')  ");
               while ($run_not_friends= mysql_fetch_assoc($not_friends)) {
                   $not_friend_id = $run_not_friends['user_id'];
                   echo $not_friend_id;
                   $i++;
               }
          }//end if
      }
 }//end while

答案 1 :(得分:0)

试试这个:

<?php

//----- get friends of friends -------


$friends_of_friends = mysql_query(" SELECT u.*
FROM (SELECT DISTINCT user_one as user_id
        FROM friends
        WHERE user_two IN (SELECT user_one as user_id
                  FROM friends
                  WHERE user_two = '$session_user_id'
                  UNION DISTINCT
                  SELECT user_two
                  FROM friends
                  WHERE user_one = '$session_user_id')
        UNION DISTINCT
    SELECT DISTINCT user_two
        FROM friends
        WHERE user_one IN (SELECT user_one as user_id
              FROM friends
                  WHERE user_two = '$session_user_id'
                  UNION DISTINCT
              SELECT user_two
              FROM friends
              WHERE user_one = '$session_user_id')
     ) f
 JOIN users u
 ON u.user_id = f.user_id  ");


  while ($run_friends= mysql_fetch_assoc($friends_of_friends)) {

  $friend_friend_id = $run_friends['user_id'];  



// ---- friends of my friends that are not my friends --------------


$check_friend_query = mysql_query("  SELECT friends_id from friends WHERE  (user_one='$session_user_id' AND user_two='$friend_friend_id') OR (user_one='$friend_friend_id' AND user_two='$session_user_id')   ");

if (mysql_num_rows($check_friend_query) != 1){ 

    $not_friends = mysql_query("SELECT `user_id`, `name`, `surname`, `email`, `profile` FROM `users` WHERE `user_id`='$friend_friend_id' AND `user_id`!='$session_user_id' LIMIT 3"); 

    while ($run_not_friends= mysql_fetch_assoc($not_friends)) {

    $not_friend_id = $run_not_friends['user_id'];

                  echo $not_friend_id;
    }

  }//end if

 }//end while

?>