我目前有三个单独的查询,我试图将它们组合在一起,因此效率更高。
我将它们放在一起的原因是我可以通过submitdate对所有结果进行排序,因为现在它们按提交日期排序,但是每个查询都会分开。
感谢有关此事的任何帮助,感谢十亿人。
首先是
查询获取我评论过的所有帖子都有新评论
SELECT DISTINCT p.*,c.submitdate as MostRecentSubmitDate
FROM posts p
INNER JOIN comments c
ON c.postid = p.id
WHERE c.submitdate > (
SELECT MAX(c2.submitdate)
FROM comments c2
WHERE c2.postid = c.postid
AND c2.deviceID = "$DeviceID"
)
第二是
在我的帖子中获得最新回复的查询。
SELECT p.PostTitle,p.id AS PostID,c1.id AS CommentID, c1.comment, q.LatestCommentDate, c1.deviceID
FROM (SELECT c.postid, MAX(c.SubmitDate) AS LatestCommentDate
FROM comments c GROUP BY c.postid) q
INNER JOIN posts p ON q.postid = p.id and "$DeviceID" = p.deviceiD
INNER JOIN comments c1 ON q.LatestCommentDate = c1.submitDate
第三是
查询获得每个帖子的投票数量
SELECT * FROM posts
WHERE DEVICEID = "$DeviceID"
AND PostVotes > 0
ORDER BY SUBMITDATE
答案 0 :(得分:1)
您可以使用UNION
将所有查询组合在一起。
制作UNION
query 1
UNION
query 2
UNION
query 3
ORDER BY ...