mysql两个查询

时间:2012-08-08 14:02:25

标签: php mysql

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

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中构建

1 个答案:

答案 0 :(得分: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