Solved

How Do I Write a Multiple Inner Join?

Posted on 2009-07-04
4
262 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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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