[英]Use a SELECT result as WHERE condition to another SELECT in the same query, is that possible?
因此,我正在使用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擁有誰在關注誰的ID,user_share卻擁有有關人們共享的內容的信息(例如新傳記,新的個人資料圖片等)。 我想要實現的目標是在我目前作為大學項目正在研究的“社交網絡”的主頁中建立一個login_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還是個新手。 謝謝。
您的細節很難理解,但是可以回答問題標題。 是的,像這樣:
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"
順便說一句,您的代碼是錯誤的WHERE users.user_id = '$follow_id[$i]'
永遠不會工作。 您正在嘗試從記錄$follow_id
獲取$i
列。 我想您正在嘗試獲取$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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.