TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-15-2007, 11:52 AM   #1 (permalink)
The Contributor
 
Join Date: Dec 2007
Posts: 27
Thanks: 0
crazyryan is on a distinguished road
Default JOINs?

I don't have a problem just for future reference.

I see alot about left joins and stuff, and I think it's used for what I'm about to explain.

Say I have something like this for games:

id - title - description - categoryid
1 - Game - Description - 1

And the category table is like this:
id - category
1 - Games


I could do a query like
select category from categories where id = 'categoryid'

or whatever, but, is there anyway i can do this in one query, so i can select all of the games stuff, and the actual category name
crazyryan is offline  
Reply With Quote
Old 12-15-2007, 12:16 PM   #2 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

yea a join would do it:
mysql Code:
SELECT games.title, catagory.name
FROM games
JOIN catagory ON ( games.id = catagory.games )
WHERE catagory.name =  'pogo'
or something to that effect
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 12-15-2007, 12:20 PM   #3 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

incidentally i'd like to post this great mysql tutorial resource, its got live examples to help you understand stuff.

JOIN tutorial:
http://sqlzoo.net/3b.htm
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 12-15-2007, 06:11 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Just remember to join on indexes. Don't join on non-indexes as it will become painfully slow.

LEFT JOIN basically says if the data is in table 1, but might not be in table 2 - if it's not in table 2, add a null and still pull table 1's data.

RIGHT JOIN is the reverse of the aforementioned.

INNER JOIN is when you only want to pull data from table 1 and table 2 when there's a valid JOIN on an index.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-15-2007, 06:38 PM   #5 (permalink)
The Acquainted
 
Join Date: Nov 2007
Posts: 154
Thanks: 31
SOCK is on a distinguished road
Default

Although I agree with Wildhoney (especially about creating an INDEX on most (if not all) columns you regularly perform joins on), I've always found the easiest way to look at the difference between a RIGHT and LEFT JOIN is by realizing that the LEFT join pulls data from the table referenced on the left side of the SQL statement, e.g.
Code:
SELECT
 t1.c1, t1.c2, t2.c1
FROM table1 AS t1 LEFT OUTER JOIN table2 AS t2
USING (c3)
Obviously table1 is on the left so all results will be pulled from table1 whereas NULL will represent columns in missing records from table2 (on the right). The RIGHT OUTER JOIN (not used as often, in my experience) would represent all records from table2 (the right hand table) with NULL representing missing values from table1.

Note that both LEFT and RIGHT join statements are implied OUTER joins, but you can specifically use the OUTER keyword to avoid confusion.

Additionally, you can think of the difference between the INNER and OUTER joins as being the result of the join itself; the INNER join returns values only from inside the set of matched records. In other words, only records will be returned where there is a match on the conditions. The OUTER join also returns records 'outside' the parameters of the match, based on the table on the left or the right. Get it?
SOCK is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:25 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design