tags:

views:

69

answers:

4

Hi

I need to get the forum name, thread name and post message with the post id 32.

Would this be possible to do with 1 query?

forums

  • id
  • name

threads

  • id
  • forumid
  • subject

posts

  • id
  • threadid
  • message
+1  A: 
select * 
from forums f 
     join threads t on f.id = t.forumid 
     join posts p on p.threadid = threads.id 
where p.id = 32
Tim Coker
Of course, it's generally a bad idea to return all columns indiscriminately, and it would be better to specify the columns required (f.name, t.subject, p.message in this instance)
Rowland Shaw
A: 

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
Vilx-
+3  A: 
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
Ignacio Vazquez-Abrams
A: 

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

Amirouche Douda