![]() |
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?
|
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 |
Quote:
|
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 | passwordCode:
emailid | address | useridIs this what you meant by shared columns? Alan |
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:Code:
emails:Code:
phones:Code:
users:Code:
emails:Code:
phones: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.phoneCode:
name: user1,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.phoneThe 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. |
what does it mean for a leftmost table and rightmost table?
|
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 |
| All times are GMT. The time now is 08:10 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0