硬连接。可能吗?

时间:2010-07-13 10:43:38

标签: sql

我需要获取论坛名称,帖子名称和帖子ID为32的帖子。

这可以用1个查询吗?

论坛

  • ID
  • 名称

线程

  • ID
  • forumid
  • 受试者

帖子

  • ID
  • 线程ID
  • 消息

4 个答案:

答案 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上测试