我有一個查詢拉所有文章從數據庫中..PHP/MySQL查詢在循環中,如何附加到一個查詢?
$get_articles = $db->query("
SELECT *, a.id AS id, s.type AS type FROM ".$prefix."articles a
LEFT JOIN ".$prefix."sources s ON (s.id = source_id)
WHERE a.type!='trashed' ORDER BY a.timestamp DESC LIMIT $start, $end");
在此查詢的循環,我不那麼做同桌另一個查詢找到相關文章的「標題」該文章存儲爲'$ related_phrase'。循環內的查詢是:
// get related articles to this entry
$get_related = $db->query("
SELECT *, a.id AS id, MATCH (title, description) AGAINST ('$related_phrase') AS score FROM ".$prefix."articles a
LEFT JOIN ".$prefix."sources s ON (s.id = source_id) WHERE a.type!='trashed' AND MATCH (title, description) AGAINST ('$related_phrase') AND a.id!='$articles[id]' HAVING score > 7
ORDER BY a.timestamp DESC LIMIT 0, 3");
這基本上意味着我們有一個查詢導致頁面加載非常緩慢的循環。
我們想要做的是在主查詢中從循環中引入查詢,因此如果可能的話,它在一個查詢中都起作用了?
非常感謝任何幫助!