gpt4 book ai didi

php - mysql 两个查询

转载 作者:行者123 更新时间:2023-11-29 06:57:31 24 4
gpt4 key购买 nike

我有一个评论系统;我有一个问题

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

最佳答案

不是对每个帖子运行一个查询,而是使用 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

关于php - mysql 两个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11866189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com