Learn MySQL joins in 5 minutes! Bro Code https://www.youtube.com/watch?v=G3lJAxg1cy8 Transkript (automatisch erstellt) 0:00 hey everybody welcome back again in today's video I'm going to be explaining joins in MySQL a join is a clause that is used to combine rows from two or more 0:09 tables based on a related column between them such as a foreign key here's an example I have two tables a table of transactions and a table of customers 0:19 think of these two tables as a Venn diagram transactions will be the table on the left customers will be the table on the right whatever data they have in 0:27 common is the middle part of our Venn diagram for my demonstration to make more sense I will need to add a few extra rows feel free to pause the video 0:35 if you need to catch up I will insert into transactions a new row the amount is one dollar the customer ID is null so not all transactions can have a customer 0:46 ID that foreign key here's a scenario suppose that somebody comes in pays for a soda with cash well we wouldn't have a customer ID right if a customer instead 0:57 paid with a credit card we could track who that customer was there may be a customer ID who initiated that credit card charge I'm going to insert this row 1:07 and here's our new transactions table not all rows have a customer ID then let's add one more customer insert into customers first name last 1:19 name will be poppy Puff let's select all of our customers now not all transactions have a customer ID 1:29 and not all customers have ever initiated a transaction they could be registered as a customer but they have never actually bought anything yet using 1:38 joins let's take a look at what data these two tables have in common we'll discuss inner joins left joins and right joins let's begin with an inner join to 1:49 create an inner join between these two tables you would type select all from which table would you like to be on the left 1:57 think of that Venn diagram our transactions table will be on the left from transactions inner join whatever table you would like to be on 2:08 the right in this case customers on we're going to join these two tables together by the foreign key from transactions we'll take 2:20 transactions.the name of the foreign key column which was customer ID equals the table on the right dot the name of the primary key column which was 2:32 customer ID then we will execute the statement and here's our new table we have created an inner join from these 2:41 two tables based solely on what they have in common if you remember we don't have that Row for that transaction for one dollar that doesn't have a customer 2:50 ID as well as our customer poppy puff there's no role for her in this table what we're telling MySQL is to select all rows from these two tables that have 3:00 matching customer ideas so that's why some data was excluded one thing you could do with a join this applies to left joins and right joins as well you 3:10 don't necessarily need to display every single column from both tables you can select specific columns let's select our transaction ID the 3:22 amount the first name then the last name 3:28 this would make it a lot easier to find the first and last name of who initiated a transaction at a given time we know who bought which order so that is an 3:39 inner join join together any matching rows based on some Link in this case we're joining these two tables together by their customer ID 3:48 now with the left join we are going to display everything from the table on the left but let's select all with the left join we will display 3:58 everything from the table on the left our transactions however if there is a matching customer ID pull in any relevant data from the table on the 4:08 right even though there's no customer ID with this latest row we're still going to display it with a left join but there's no data to pull in from the 4:16 right table because there's no registered customer ID with the right join we will display the entire table on the 4:24 right if there's any matches we will pull in any matching rows from the left we are displaying all of our customers if any of these customers ever initiated 4:34 a transaction we will include the data from those transactions we still have poppy puff in our table but she has no relevant transactions she never 4:44 initiated one so yeah everybody those are joins a join is used to combine rows from two or more tables based on a related column between them such as a 4:54 foreign key like customer ID and that is a quick introduction to joins in MySQL