php - MySQL 从两个表的 parentid 获取标题

标签 php mysql

我会尽量解释清楚。这是我的查询..

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

我面临的挑战是让它与我的评级表一起工作,其中为 BC 设置了 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/

相关文章:

php - while 内选择的属性

php - 如何在 Javascript 中解析 "Mysql result JSON String"?

php - 解决 php-common 冲突的最佳方法 : ignore, 修复,其他?

php - 当包含来自多个目录的相同 css 文件时如何处理 CSS 中的路径

php - 提交到MySQL,但if语句导致问题

需要返回负集的mysql连接查询

php - 连接 2 个表 X 和 Y 以显示 Total sum() 结果事件表 Y 为空

php - 在 html 中使用 php 来显示数据库

mysql - 如何使用 CASE 执行 SQL 请求

php - 警告 : mysqli_connect(): (HY000/2002): No such file or directory