将SELECT结果作为WHERE条件用于同一查询中的另一个SELECT,这可能吗?

时间:2015-05-08 17:36:36

标签: php mysql mysqli

所以,我正在使用3个表:users,follow和user_share。

Users: 

user_id | username |  first_name | last_name | 
______________________
1       | a        | ....
2       | b        | ....
3       | c        | ....
4       | d        | ....
5       | e        | ....
......................  

Follow:  

id | follower |  followed | 
______________________
1  | 20        | ....
2  | 20        | ....
3  | 12        | ....
4  | 22        | ....
5  | 77        | ....
......................
User_share:  

user_id | share_id |  share_type | share_path | share_flag 
______________________
12       | 1        | ....
22       | 2        | ....
22       | 3        | ....
12       | 4        | ....
4        | 5        | ....
...................... 

关注持有ID'谁跟踪谁,user_share反而拥有人们共享的信息(例如新的传记,新的个人资料图像等)。我想要实现的目标是在一个社交网络的主页中构建一个logged_in部分'我目前正在做大学项目。为了做到这一点,我需要获得我将要告诉每个人跟随某人(即用户举行会议)的所有信息。
这就是我到目前为止所得到的:

$conn=mysqli_connect('localhost', 'root', '', 'database');
              // return the id of all the people followed by the session user
              $result = mysqli_query($conn, "SELECT `followed` FROM `follow` WHERE `follower` = '$session_user_id'");
              $follow_id = mysqli_fetch_array($result, MYSQLI_NUM);
              $id_length = count($follow_id);
              for($i = 0; $i < $length; $i++){
              //return the information about every people followed
              $data = mysqli_query($conn,
    "
SELECT u.username
     , u.first_name
     , u.last_name
     , u.user_id
     , s.share_type type
     , s.share_path path
     , s.shared_flag flag
     , s.share_id
  FROM users u
  JOIN user_share s
    ON u.user_id = s.user_id
 WHERE u.user_id = '$follow_id[$i]'
 ORDER 
    BY user_share.share_id
    "
                                      );
               /*Within this loop I'm gonna process the information of data() and eventually output them*/                       
              }  

我只是想知道:我可以将上述两个查询合并到一个查询中吗?类似于嵌套的SELECT,其中第一个结果在第二个SELECT中用作WHERE条件。我甚至不知道这是一个愚蠢的问题,我是MySQL的新手。谢谢。

2 个答案:

答案 0 :(得分:3)

您的详细信息难以理解,但问题标题的答案是否正确;是的,就像这样:

SELECT *
FROM aTable
WHERE (field1, field2) IN (
   SELECT [corresponding field list] 
   FROM ....
   )
;

答案 1 :(得分:3)

"SELECT f.followed,
    u.username, 
    u.first_name, 
    u.last_name, 
    u.user_id, 
    user_share.share_type AS type, 
    user_share.share_path AS path, 
    user_share.shared_flag AS flag, 
    user_share.share_id
FROM follow f
LEFT JOIN users u
ON f.followed = u.user_id
INNER JOIN user_share us
ON u.user_id = us.user_id
WHERE follower = '$session_user_id'
ORDER BY f.followed, us.share_id"

顺便说一下,你的代码错误WHERE users.user_id = '$follow_id[$i]'永远不会有效。您正试图从记录$i获取$follow_id列。 我猜你试图获得$i记录。

这是另一个错误:

 $id_length = count($follow_id);
 for($i = 0; $i < $length; $i++){

所以你有$id_length,但条件是你使用$i < $length

所以为了简化你的生活,我会把这段代码作为一个起点:

$conn = new PDO('mysql:dbname=database;host=localhost', 'root', '');
$query = "SELECT f.followed,
    u.username, 
    u.first_name, 
    u.last_name, 
    u.user_id, 
    user_share.share_type AS type, 
    user_share.share_path AS path, 
    user_share.shared_flag AS flag, 
    user_share.share_id
FROM follow f
LEFT JOIN users u
ON f.followed = u.user_id
INNER JOIN user_share us
ON u.user_id = us.user_id
WHERE follower = ?
ORDER BY us.share_id";
if ($stmt = $conn->prepare($query)) {
    $stmt->bindParam(1, $session_user_id);
    $stmt->execute();
    while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        print_r($row);
    }
}