我需要获取论坛名称,帖子名称和帖子ID为32的帖子。
这可以用1个查询吗?
论坛
线程
帖子
答案 0 :(得分:3)
SELECT f.name, t.subject, p.message
FROM posts AS p
INNER JOIN threads AS t
ON p.threadid = t.id
INNER JOIN forums AS f
ON t.forumid = f.id
WHERE p.id = 32
答案 1 :(得分:1)
select *
from forums f
join threads t on f.id = t.forumid
join posts p on p.threadid = threads.id
where p.id = 32
答案 2 :(得分:0)
有什么问题?
select name, subject, message from posts join threads on (threadid=posts.id) join forums on (forumid=forums.id) where posts.id=32
答案 3 :(得分:0)
我添加了帖子的ID以查看行
SELECT p.id ,p.message,t.subject, f.name
FROM forums AS f
RIGHT OUTER JOIN threads AS t
ON f.id = t.forumid
RIGHT OUTER JOIN posts AS p
ON t.id = p.threadid
WHERE p.id = 32
在MySql上测试