When you start dealing with large sets of data, you really need to think about your database design and how it will scale in size and for different purposes in the future. Joins can help you keep things organised and flexible when you're dealing with "sets" of data.
Suppose you have a table containing a list of foods, and a table containing a list of people...
Now we want to keep track of which users like which type of foods. They are given a choice to pick which ones they prefer, and we're going to use some "mapping" to associate them with the foods.
Here's another table, called foodMap, which has the ID of the food chosen, and the ID of the user who chose it.
To get a list of the user "Stans" favourite foods, can use a fairly simple JOIN query such as...
SELECT foods.nameFROM foodMapLEFT JOIN foods ON foodMap.foodID=foods.idWHERE foodMap.userID=1
Basically, we're selecting all the rows stored in the "foodMap" table, and attaching to the left of that, the food name. The join pivots on the foodID in the foodMap table being equal to the foods tables id field. If there is no match, then that will not be selected.
#1 Josh says:
Dont know if you were going to touch base on this in another article, but just thought I'd point out that you should definetely add indexes on your relational table and for the corresponding fields in the related tables as joins can increase cpu load, especially when querying large amounts of data.
And yes that was a very long sentence.