开发者

Hard join. Is it possible?

I need to get the forum name, thread name and post message with the post id 3开发者_运维技巧2.

Would this be possible to do with 1 query?

forums

  • id
  • name

threads

  • id
  • forumid
  • subject

posts

  • id
  • threadid
  • message


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


select * 
from forums f 
     join threads t on f.id = t.forumid 
     join posts p on p.threadid = threads.id 
where p.id = 32


What's the problem?

select name, subject, message from posts join threads on (threadid=posts.id) join forums on (forumid=forums.id) where posts.id=32


I added the id of the post to see well the row

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

Tested on MySql

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜