Zum Inhalt springen
L

Das Video kommt von YouTube: erst beim Abspielen verbindet sich die Seite mit YouTube (Google).

Learn MySQL joins in 5 minutes!

Bro Code5:04 980.179 Aufrufe veröffentlicht Auf YouTube

Das Wichtigste aus dem Video

Tipp auf eine Zeit – das Video springt genau dorthin.

Transkriptautomatisch erstellt · 31 Zeilen
Herunterladen
  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. name will be poppy Puff let's select all of our customers now not all transactions have a customer ID
  10. 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
  11. 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
  12. create an inner join between these two tables you would type select all from which table would you like to be on the left
  13. 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
  14. 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
  15. 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
  16. customer ID then we will execute the statement and here's our new table we have created an inner join from these
  17. 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
  18. 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
  19. 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
  20. don't necessarily need to display every single column from both tables you can select specific columns let's select our transaction ID the
  21. amount the first name then the last name
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. right table because there's no registered customer ID with the right join we will display the entire table on the
  28. 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
  29. 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
  30. 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
  31. foreign key like customer ID and that is a quick introduction to joins in MySQL

Zum Nachlesen