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
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 02-09-2008, 05:52 PM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Location: On your Hard Drive, hiding like a Virus
Posts: 818
Thanks: 163
Orc is on a distinguished road
Default Tables joining other Tables?

How would I join tables with other tables, say if I wanted to create something needed another table, how would I bring that tables columns, rows, values into the current table?
__________________
Wax on, Wax off
Orc is offline  
Reply With Quote
Old 02-09-2008, 06:41 PM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 482
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Joins can be a huge pain sometimes when you are trying to get the correct data from multiple tables

Have a read of this tutorial which should get you started - it gives a good introduction to selecting / updating data across multiple tables in MySQL.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following 2 Users Say Thank You to Alan @ CIT For This Useful Post:
Orc (02-09-2008), ReSpawN (02-10-2008)
Old 02-09-2008, 06:43 PM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Location: On your Hard Drive, hiding like a Virus
Posts: 818
Thanks: 163
Orc is on a distinguished road
Default

Quote:
Originally Posted by Alan @ CIT View Post
Joins can be a huge pain sometimes when you are trying to get the correct data from multiple tables

Have a read of this tutorial which should get you started - it gives a good introduction to selecting / updating data across multiple tables in MySQL.

Alan
So shared columns can only work when joining stuff? Cause that's what I sorta had in mind.
__________________
Wax on, Wax off
Orc is offline  
Reply With Quote
Old 02-09-2008, 07:23 PM   #4 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 482
Thanks: 51
Alan @ CIT is on a distinguished road
Default

I'm not sure what you mean by shared columns.

The idea behind using joins is to split data across multiple tables. For example, say you have a users table:

Code:
userid | username | password
----------------------------
1      | alan     | secret
2      | orc      | something
And you decide to store the users email addresses in a seperate 'emails' table:

Code:
emailid | address | userid
--------------------------
...more rows here...
15       | alan@example.com | 1
16       | orc@example.com  | 2
You can see that the 'userid' column in our 'users' table, matches the 'userid' column in our 'emails' table. If we wanted to fetch a username and their email address, we would do a join across the two tables using the 'userid' column to get the data we wanted.

Is this what you meant by shared columns?

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 02-09-2008, 07:59 PM   #5 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 354
Thanks: 3
xenon is on a distinguished road
Default

Bear these advices with you:

- you want to get data from different tables, but you need each table to return some data -> use an inner join
- you want to get information that's required to be only in the leftmost table -> use a left join
- you want to get information that's required to be only in the rightmost table -> use a right join.

Perhaps these will create some confusion, that's why I need to come up with some examples. Let's start with 3 very simple tables: users, emails and phones. The structures are, as follows:

Code:
users:
- id (int 8 auto increment)
- name (varchar 64)
Code:
emails:
- id (int 8 auto increment)
- email (varchar 128)
- user_id (int 8, index)
Code:
phones:
- id (int 8 auto increment)
- phone (varchar 128)
- user_id (int 8, index)
Now, the sample data.

Code:
users:
1, user1
2, user2
3, user3
Code:
emails:
1, user1@website.com, 1
2, user2@website.com, 2
Code:
phones:
 1, user1.111.222, 1
 2, user3.001.002, 3
As you can see, user1 has all data filled in. However, user2 has only an e-mail and user3 has only a phone number.

INNER JOIN (this will fetch data for a user that has information in all tables used in the join):

Code:
SELECT u.name, e.email, p.phone
FROM users u
INNER JOIN emails e ON(u.id=e.user_id)
INNER JOIN phones p ON(u.id=p.user_id)
will return:

Code:
name: user1,
email: user1@website.com,
phone: user1.111.222
If we remove the e-mail for the user1, then we will not have any rows returned, unless we remove the email field from the join, or we change the join type.

LEFT JOIN (this will fetch data from all tables, but only the first table used in the join must contain some rows):

Code:
SELECT u.name, e.email, p.phone
FROM users u
LEFT  JOIN emails e ON ( u.id = e.user_id ) 
LEFT  JOIN phones p ON ( u.id = p.user_id )
This query will return all the 3 users, but the phone field will be empty for the user2 (NULL), and the e-mail field for the 3rd will be NULL, aswell. However, the rows ARE returned, because all 3 users exist in the leftmost table used in the join.

The RIGHT join is the same with the left, only switch the leftmost with the rightmost table. If you run it, the 1st and 3rd users will be returned.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 02-09-2008, 08:04 PM   #6 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Location: On your Hard Drive, hiding like a Virus
Posts: 818
Thanks: 163
Orc is on a distinguished road
Default

what does it mean for a leftmost table and rightmost table?
__________________
Wax on, Wax off
Orc is offline  
Reply With Quote
Old 02-09-2008, 09:42 PM   #7 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 354
Thanks: 3
xenon is on a distinguished road
Default

leftmost - the table you're joining first
rightmost - the table you're joining last

If you look at a query, you'll see that I referred to them by the position in the query.

Code:
SELECT m.*, n.*, k.* FROM table1 m LEFT JOIN table2 n LEFT JOIN table3 k
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
Orc (02-09-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 10:43 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0