Solved

How Do I Write a Multiple Inner Join?

Posted on 2009-07-04
4
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Comparison query - 4 columns 9 50
Current Month Filter in Visual Studio 10 46
CRM 2011 Advanced Find Producing Error 3 36
how to use ROW_NUMBER() correctly 8 40
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

738 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