tags:

views:

115

answers:

4

Here i need help with joins. I have two tables say articles and users. while displaying articles i need to display also the user info like username, etc. So will it be better if i just use joins to join the articles and user tables to fetch the user info while displaying articles like below.

SELECT a.*,u.username,u.id FROM articles a JOIN users u ON u.id=a.user_id

OR can this one in php. First i get the articles with below sql

SELECT * FROM articles

Then after i get the articles array i loop though it and get the user info inside each loop like below

SELECT username, id FROM users WHERE id='".$articles->user_id."';

Which is better can i have explanation on why too. Thank you for any reply or views

+4  A: 

There is a third option. You could first get the articles:

 SELECT * FROM articles

Then get all the relevant user names in one go:

 SELECT id, username FROM users WHERE id IN (3, 7, 19, 34, ...)

This way you only have to hit the database twice instead of many times, but you don't get duplicated data. Having said that, it seems that you don't have that much duplicated data in your queries anyway so the first query would work fine too in this specific case.

I'd probably choose your first option in this specific case because of its simplicity, but if you need more information for each user then go with the third option. I'd probably not choose your second option as it is neither the fastest nor the simplest.

Mark Byers
How would i display the users info doing this way? if say i have to show userinfo from article id 1.
askkirati
Convert the results in associative arrays first, then iterate over the articles array using a lookup in the users array to get the user info as needed.
Mark Byers
Hello can you elaborate it a bit will say php codes?
askkirati
+2  A: 

The first approach is better if applicable/possible:

SELECT a.*,u.username,u.id FROM articles a JOIN users u ON u.id=a.user_id
  • You have to write less code
  • There is no need to run multiple quries
  • Using joins is ideal when possible
Sarfraz
+1. There may be reasons to go the other route (selecting all the users at once as one answer stated is a smart move too), but MySQL has put time into optimizing joins and if you are indexed correct it should be pretty fast and you can cache the results easily in several ways. Additionally, I find more and more that throwing more CPU/RAM is easier than maintaining a highly optimized but challenging-to-maintain code base. I totally lean toward "squeeze every last nickel out of my servers" but it's really not worth it and my psychiatrist and the guys at Tweakers-Anon have been helping me a lot.
Hans
@Hans: yup i'm agreed to your point of view :)
Sarfraz
I am confused to the duplicate data folks are bringing out here. Do we get duplicate data while doing joins? Also how would i select the users in one go after i select the articles. If you guys understand i am doing this in php using foreach loop for showing articles in a page.thanks
askkirati
You will duplicate the joined table - in this case you will receive (for example) `user id 10, name Henry, email [email protected]` for every article written by Henry
Andy
ok now understood. thnx alex
askkirati
+3  A: 

It depends how much data the queries are returning - if you'll be getting a lot of duplicate data (i.e. one user has written many articles) you are better off doing the queries separately.

If you don't have a lot of duplicated data, joins are always preferable as you only have to make one visit to the database server.

Andy
Yes a user can have many articles. it has has many relations i think. A user can have many articles. and one article will be related to single user only. So users has many relation to articles
askkirati
A: 

Get the articles with one query, then get each username once and not every time you display it (cache them in an array or whatever).

Lo'oris