1

我有一个评论系统;我有一个查询

SELECT ShoutID,Message, User, Date, ShoutImageThumb, ShoutImageBig,UserID, 
       Email, FullName, UserImageThumb, Admin FROM shoutbox LEFT JOIN users ON 
       users.Email = shoutbox.User ORDER BY ShoutID DESC LIMIT 10

当它通过页面上的循环时,我在循环中运行另一个查询以获取该消息的评论

SELECT users.UserID, users.Email, users.FullName, shoutboxcomments.ShoutID, 
       shoutboxcomments.`Comment`, shoutboxcomments.`Date`, 
       shoutboxcomments.`User`, shoutboxcomments.CommentID FROM shoutboxcomments 
       LEFT JOIN users ON users.Email = shoutboxcomments.User 
       WHERE ShoutID = '.$ShoutID.' ORDER BY CommentID ASC

是否可以加入这些查询或如何简化它,因为我使用 mvc 架构在 php 中构建

4

1 回答 1

2

无需为每个帖子运行一个查询,而是一次IN获取多个帖子的评论。

SELECT users.UserID, users.Email, ...
FROM shoutboxcomments
LEFT JOIN users
ON users.Email = shoutboxcomments.User
WHERE ShoutID IN (1, 4, 15, 22)
ORDER BY CommentID
于 2012-08-08T14:04:50.290 回答