join

MySQL tutorial 16 - joining many tables



Sharing buttons:

hey YouTube how's it goin it's Quinton

here and welcome to this tutorial on

joining tables and yeah in the previous

tutorial we take took a look at joining

two different tables but I just wanted

to take a little bit more of an in-depth

look at this because in that tutorial we

only join two tables and yeah you can

actually join more than two tables so I

just wanted to give you guys an example

of how that works but before I start off

this tutorial I do want to send a huge

shout out to my sponsors at dev Mountain

dev Mountain is a coding bootcamp

that'll teach you everything you need to

know to become a web developer and iOS

developer or a UX designer and they'll

do it within 12 weeks so go ahead and

check out their website the link is in

the video description below and if you

do contact them make sure to tell them

that I sent you and now we can get love

with the tutorial so go over to the

learning over here database that we

imported in the first tutorial and

there's actually a very very specific

table that I created exactly for this

tutorial which is the orders table so

click on that and if you inspect this

orders table you'll see that there is an

order ID a product ID a user ID and an

address ID so basically this table has

just full of IDs there's no actual

information in here and if we browse

this you can see that yeah it's

basically just the table with a bunch of

numbers in it so there's not really much

information that can be sourced out of

this table

so this is where joins perform their

best and yeah it basically means that

all of the information that's in the

other tables I did not have to repeat

when I put when I created this table

basically I just grab the IDS of the

other tables and I put them into this

table and now I can gather any

information I'd like from those tables

whenever a viewing in order so basically

I can see that I have an order over here

and this is order with an ID of 1 and if

I wanted to see what product they

ordered I could link to the product

table and get any information about that

product just using that product ID I can

also link to the users table and get any

info

nation about that user using the user ID

and of course I can grab the address

that that user asked the product to be

delivered to by checking out what's in

the addresses table and using the

address ID so now let's take a look at

where this would be useful and I'm going

to write a query over here so go ahead

and just remove everything that's in

here at the moment and let's type select

and I think let's start off by just

selecting from two tables because that's

how I like to build my queries is just

like try keep them simple and then add

more and more stuff on as it gets

complicated although this is going to be

a very easy tutorial so don't be dumb be

scared but yeah let's say select and

we'll select the orders dot order ID so

now we're selecting something from our

orders table and I think the next thing

we should select from is going to be

from the products table so let's select

the product name so you can see what the

what what order was placed and what

product they ordered so let's select

products dot product name so now we've

got our two fields we can type in the

from keyword and then I can select the

first table which is going to be orders

and then join on my second table which

is going to be addresses and I want to

join these where the whoops I'm going to

be addresses it's going to be products I

totally spaced out there for a second

but yeah it's going to be products and I

want to join these tables where the

orders dot product ID is equal to the

products dot product ID so let's go on

orders dot product ID is equal to

product dot product ID because those are

the two fields that are in both of these

tables and now let's copy this and hit

go and now you can see that we have an

order ID with an order of one so our

first order went to or was for a blue

lightsaber and now I think it'd be kind

of interesting

to see who this order went to so I'd

like to add on information from the

users table because we know that the

information about who this would be

going to is going to be in the users

table so let's go over to the SQL tab

here and let's copy my query back in

here and to add on another table is

super super simple all I do is I add in

a comma over here and I select the next

field that I want to select so in this

case I want to select something from the

users table and I think we can select

the users first name so go ahead and

select something from the users table in

this case I want to use the first name

and then just add in another join at the

end of your query so basically we've got

this first join here which joins the

products products table and now we'll

have a second join that's going to join

the users table and that's going to join

it on where orders dot user ID is equal

to users dot ID right and again the

reason why I'm using that field is

because our orders table has a user ID

and our users table also has a user's ID

so it's the field that links both of

those tables and let's hit go so let's

just ignore that text and move on and

we've already selected from three

different tables so order I orders ID

from the orders table product name from

the products table first name from the

users table and the last table that we

have yet to select something from is the

addresses table so let's go back here

and add in something from the addresses

table so I'm going to add in a comma and

let's select the addresses city and

we'll simply join that where the address

well where the orders dot address ID is

equal to addresses dot address ID and

now let's copy this

and hopefully I don't have any errors so

let's hit go and I okay yeah I have made

a really stupid mistake yet join we want

to join addresses on orders dot address

ID yeah okay make sure that you follow

that exact same syntax and now let me go

ahead and copy this just in case I want

to write this query again and hit go and

it looks like I don't have any errors

this time so and now I can see that our

first order was a blue line saver that

went to Luke and it's going to Naboo and

the same thing with our second order

that's a stormtrooper uniform that's

also going to Luke and it's also going

to Naboo what's he up to I wonder and

yeah that is how to join four different

tables together using my SQL and

hopefully you find this tutorial helpful

and I hope that you'll go around using

the information that you learned in this

tutorial to join as many different

tables as you can but before I end off

this video I just want to send a huge

shout out to these guys so these guys

are my patrons that contribute five

dollars or more on patreon every single

month and that goes a long way to

helping me make more videos for you guys

more often so if you like the videos I

create and you want to see more then

become a patron and if you can't afford

to give away any money then just

subscribe leave a comment like and share

this video because that's also going to

help my channel grow and I'll see you

guys in the next one