我需要從另一個表中獲取帖子圖像的多個帖子,每個帖子都有不同的圖像數量。像POST1有10幅圖像,POST2有5張圖片mysql - 在子查詢中獲取多行
我的兩個表是image
和post
表post
結構
|title |desc |date |postid|...
|title1|desc1|date1| 1 |...
|title2|desc2|date2| 2 |
.
.
.
表image
結構
|hash |hits |timestamp |userid |postid|...
|hash1 |hits1|timestamp1|userid1| 1 |...
|hash2 |hits2|timestamp2|userid1| 3 |...
|hash3 |hits3|timestamp3|userid1| 2 |...
|hash4 |hits4|timestamp4|userid1| 1 |...
.
.
.
我需要獲取職位與他們的圖像,postid
是獲取圖像的帖子的關鍵。
我這樣做,但它不給我正確的結果。
SELECT `title`,`desc`,`date` FROM `img`.`post` AS a
INNER JOIN
(SELECT `hash`,`hits`,`timestamp`,`userid`,`postid` FROM `img`.`image` WHERE `postid` IS NOT NULL)
AS b
WHERE a.`postid` IS NOT NULL
我得到的結果爲
mysqli_stmt_bind_result($prepare, $title,$desc,$date);
不是
mysqli_stmt_bind_result($prepare, $title,$desc,$date,$hash,$hits,$timestamp,$userid,$postid);
這給了錯誤,因爲沒有綁定變量的不匹配。
我需要得到後的圖像陣列的
$hash,$hits,$timestamp,$userid,$postid
請參閱和建議的方式來做到這一點。
您需要包括所有來自主查詢的'SELECT'子句中的子查詢的列。 – Barmar
@Barmar能否提供一個例子來說明如何去做。 –
爲什麼'images'表中的所有行都相同,除了postid? – Barmar