Solved

How Do I Write a Multiple Inner Join?

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

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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now