我会尽量解释清楚。这是我的查询..
SELECT * FROM mm_star_ratings s
JOIN mm_posts p ON s.post_id = p.postid
WHERE p.type='B'
ORDER BY s.rating DESC LIMIT 5
type='B'
是主要帖子,type='C'
是该帖子的回复。 mm_posts
中有一个parentid
列,用于为C
设置主要帖子(B)
id。如果 type='C'
仍然存在,我是否想从 mm_posts
获取 title
?
我面临的挑战是让它与我的评级表一起工作,其中为 B
和 C
设置了 post_id
最佳答案
您可以使用以下查询,
SELECT s.*,p.*, IF(p.type='C',pp.title,p.title) as title FROM mm_star_ratings s
JOIN mm_posts p ON s.post_id = p.postid Left Join mm_posts pp on pp.postid=p.parent_id
ORDER BY s.rating DESC LIMIT 5
关于php - MySQL 从两个表的 parentid 获取标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43204278/