Solved

How Do I Write a Multiple Inner Join?

Posted on 2009-07-04
4
259 Views
Last Modified: 2012-05-07
Hi, I know this type of question has been asked and answered many times, and I've looked through numerous threads, but everything I've tried hasn't worked for me.  Obviously, I'm doing it wrong, but I haven't been able to figure it out, so I thought I'd ask it once again.  I apologize in advance.

I have three tables that I need to join in my query, a comments table, a photos table, and a users table.  I need to return a list of comments, the name of the user who made them, and the name of the photo that the comment was made on.

The comments table contains the user id, and the photo id.  Here's the query I have right now, which gets the comment, and the photo title.  I just need to add an inner join to get the user name as well:

$sql="Select * From CmtsSharedPhoto c INNER JOIN sharePhotoFiles p ON cmt_object_id = medID order by cmt_time desc LIMIT 100";

The user id in the comments table is "cmt_author_id", and in the users table (Profiles), the user id is "ID".

If you need anything else, just let me know!  Thanks!!
0
Comment
Question by:sbcjr
  • 2
4 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 350 total points
ID: 24777778
Select *, u.userName From CmtsSharedPhoto c INNER JOIN sharePhotoFiles p ON cmt_object_id = medID
INNER JOIN Profiles u on u.id = c.cmt_author_id
order by cmt_time desc LIMIT 100
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 24777780
this is mysql, right?

you will need to take the habit of:
* avoid using SELECT *. list the columns you really need
* use table_name_or_alias.column_name syntax. make it clean on all the parts of the query where a column comes from

for example:
$sql="Select c.cmt_time CommentTime, c.cmt_text CommentText, p.Name PhotoName, u.name UserName From CmtsSharedPhoto c JOIN sharePhotoFiles p ON c.cmt_object_id = p.medID JOIN users_table u on u.id = c.user_id order by c.cmt_time desc LIMIT 100";

Open in new window

0
 
LVL 1

Author Comment

by:sbcjr
ID: 24777799
Awesome!  I have no idea why I couldn't figure that out.  Thank you both so very much.  You have made my week!
0
 
LVL 1

Author Comment

by:sbcjr
ID: 24777843
@angelIII - Just a follow-up... I made the changes you suggested, and, wow!  That really reduced the loading time of these pages.  Thank you, thank you, thank you!
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help Required 3 106
convert null in sql server 12 45
Optimize the query 5 42
table joins in qry 17 58
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question