Jump to content

[SOLVED] to join or not to join?…


jonniejoejonson

Recommended Posts

Please can you help me by telling when I should and should not join 2 tables?…

Is it only when you want to compare things in 2 tables that you would use a join?… or would you also do it for the following problem…

 

For example if I had

 

Table 1.

A 'Comments' table… including a field ‘userId’… the Id of the user that leaves a comment

 

Table 2.

'ProfileDetails' table… including a field ‘userId‘ and ‘profileThumbnail’… (filename of profile picture.)

 

 

Now I need to get the correct profile pictures for the userId’s, of certain users that are in the comments table.

Do I somehow join the 2 tables…. Which I don’t see as necessary as I am not comparing any of the data?….

Or do I do 2 queries…

1. grab my userId’s from the commentsTable.

2. Grab the profileThumbnails from these grabbed userIds

 

I am self taught and am not really sure when to join tables and when just to do 2 queries… thanks to any responders.

 

Link to comment
Share on other sites

SELECT c.comment, c.userID, p.profileThumbnail FROM Comments AS c INNER JOIN ProfileDetails AS p USING (userID) WHERE c.commentID = 1

 

One query to give you a comment text, a user ID and a thumbnail for comment #1 in your table...

 

So yes: join.

Link to comment
Share on other sites

In most cases joining is preferable, as it will get you results quicker.

 

Joining can be tricky, but it's quite easy to grasp the basics.

 

In the example above, its just looking for a comment with commentID =1 in Comments table, and then tries to find matching userId in ProfileDetails table.

 

Link to comment
Share on other sites

Thanks again Mchl,

Is there much difference between the amount of time it takes to run the first and second example?... i.e. should i change the field names so that i can use the first query example?, or it doesn't make any difference.

 

I think there is quite alot for my brain to get around on joining tables... anyway thanks again for your help, much appreciated.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.