所以我有一個顯示圖像和信息的網頁,我也有三張表。如何使用不相關的數據連接三個MySQL表?
IMAGES USERS COMMENTS
id user_id id
user_id username user_id
username password comment
title isadmin date
image (location) points
description signup_date
points email
category city
bio
profile_image
我需要使用圖像和評論表中的所有信息。但我也需要檢索發佈評論和圖片的用戶的profile_image。我不知道如何做到這一點:這是我迄今爲止。
$conn = new PDO ("mysql:host=localhost;dbname=project", "root", "pass");
$stmt = $conn->prepare("
SELECT images.*, users.profile_image, comments.*
FROM (images.id JOIN comments ON images.id = comments.id)
LEFT JOIN users
ON images.user_id = user.profile_image
");
我關閉了嗎?或在這裏嘗試不可能的!?
所以你的目標是讓所有與它的用戶的信息和圖像的意見嗎? – Beginner
如果你問我,我會得到一個新的查詢意見。第一個查詢獲取圖像+用戶數據第二個查詢獲取註釋和用戶數據 –
我不認爲這三個表完全不相關;用戶的ID肯定出現在每個表格中。但我不清楚我們如何將評論與其他兩張表相關聯。它幾乎看起來好像某個外鍵丟失了。 –