Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How Do I Write a Multiple Inner Join?

Posted on 2009-07-04
4
Medium Priority
?
265 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 1400 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 600 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

618 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