Monday, 19 August 2013

Proper Syntax for 3 table SELECT query

Proper Syntax for 3 table SELECT query

I've got 3 tables:
tblPosts tblComments tblUsers
I'm trying to get a listing of Posts along with associated Comments. The
tricky part seems to be getting the Posts and Comments to show the proper
author (User). This is the closest I get but then the Posts authors are
incorrect. I'm grouping my CFOutput on "pid" so I only get each post one
time as I would expect.
SELECT tblPosts.pid, tblPosts.title, tblPosts.description, tblPosts.price,
tblPosts.datecreated AS pdate, tblPosts.image1,
tblComments.comment, tblComments.datecreated AS cdate, tblUsers.fname,
tblUsers.lname
FROM tblPosts
LEFT JOIN tblComments
ON tblPosts.pid = tblComments.pid
LEFT JOIN tblUsers
ON tblComments.uid = tblUsers.uid
Any thoughts? Thanks!

No comments:

Post a Comment