简体   繁体   English

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

[英]Use a SELECT result as WHERE condition to another SELECT in the same query, is that possible?

So, I'm working with 3 table: users, follow and user_share. 因此,我正在使用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        | ....
......................

Follow holds the ids' of who is following who, user_share instead holds the information about what people did shared (such as a new biography, a new profile image etc). Follow拥有谁在关注谁的ID,user_share却拥有有关人们共享的内容的信息(例如新传记,新的个人资料图片等)。 What I'm trying to achieve is building a logged_in section into the home page of a 'social network' I'm currently working on as college project. 我想要实现的目标是在我目前作为大学项目正在研究的“社交网络”的主页中建立一个login_in部分。 In order to do that I need to get every information I'm gonna show up about every people followed by someone (ie user holding the session). 为了做到这一点,我需要获取所有信息,我将向每个人显示某个人(例如,参加会议的用户)。
That's what I got so far: 到目前为止,这就是我得到的:

$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*/                       
              }  

I'm just wondering: can I incorporate the two query above in a single query? 我只是在想:我可以将上面的两个查询合并到一个查询中吗? Something like a nested SELECT where the first result is used as WHERE condition in the second SELECT. 类似于嵌套的SELECT,其中第一个结果在第二个SELECT中用作WHERE条件。 I don't even know if this is a stupid question,I'm quite newbie to MySQL. 我什至不知道这是否是一个愚蠢的问题,我对MySQL还是个新手。 Thanks. 谢谢。

Your detail is hard to follow, but the answer the question header; 您的细节很难理解,但是可以回答问题标题。 yes, like so: 是的,像这样:

SELECT *
FROM aTable
WHERE (field1, field2) IN (
   SELECT [corresponding field list] 
   FROM ....
   )
;
"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"

And by the way your code is wrong WHERE users.user_id = '$follow_id[$i]' will not work never. 顺便说一句,您的代码是错误的WHERE users.user_id = '$follow_id[$i]'永远不会工作。 you are trying to get $i column from the record $follow_id . 您正在尝试从记录$follow_id获取$i列。 I guess you were trying to get $i record. 我想您正在尝试获取$i记录。

And here is another error: 这是另一个错误:

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

So you have $id_length but in condition you use $i < $length . 因此,您有$id_length但在条件下使用$i < $length

So just to simplify your life a little I would offer this code as a start point: 因此,为了简化您的生活,我将提供以下代码作为起点:

$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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM