Joining Stores and Addresses0:00
Joins allow us to connect, or intersect, any number of tables. I'll show you an example. Here we have a stores table. We have two different stores. And notice that a store, of course, has an address. Which means the store with an ID of 1 has an address of 47, my Sequila Drive. So we could represent this as an SQL query by saying, give me everything from the stores table. And if we run it, we get those two records. But now, I'd like the full address to be joined in as part of this information here.
And if we run it, we get those two records. But now, I'd like the full address to be joined in as part of this information here. Alright, that's what we use a join for. So hmm, let's see. I have my $storeQuery. I want to join in the results from the addresses table. But we need some kind of connecting tissue. In other words, what would connect this record to this record? Well we've already learned this, the foreign key. So I could say, join the addresses table on the condition that the stores.address_id
Using Foreign Keys in Joins0:54
Well we've already learned this, the foreign key. So I could say, join the addresses table on the condition that the stores address_id is equal to the ID on the addresses table. So now if I run it, we still get the same two records, but this time the address is included. So I hope that makes sense. And the keyword here is on. So whenever you write on in your head, think to yourself on the condition. Let's read it all. Collect everything from the stores table, but join the addresses table on the condition.
Join Type Variants Overview1:21
Let's read it all. Collect everything from the stores table, but join the addresses table on the condition that these two match up. That is the connecting tissue. Okay, so that's effectively a join. But it gets a little more tricky because in the wild, you've likely seen many variants. You've probably seen inner join, or left join, or left outer join, or right join, or right outer join. And there's even more than that. So what is the difference?
Left vs Right Join Behavior2:55
Same thing again. And this is where, from my experiences, it can get a little overwhelming because you think there's too many variants and I can't remember the differences. Yes, you can. So just as JOIN and INNER JOIN are the same thing, well, LEFT JOIN and LEFT OUTER JOIN are the same thing as well. So when you write LEFT JOIN, it translates to, in MySQL, a LEFT OUTER JOIN. So if I give that a run, it's not immediately clear here what the difference is. But if I change it to RIGHT OUTER JOIN, OK, now it is clear. So notice I got 604 results.
If there's no match, I don't want to see it. With a left join, we're saying, on the condition that there's no match, I want to favor the left side. And that's how I often think of it. You have the left side of this join keyword, which would be the stores table, and then the right side of the join keyword, which is the addresses table. So if there is no match, favor the stores table. I always want to see every record from that table. But if I change this to right, we've switched it. If there's no match, favor the right side of the join keyword.
