课程: SQL Hands-On Practice: Solve Business Problems

免费学习该课程!

今天就开通帐号,24,700 门业界名师课程任您挑!

Relating users and admins using a self join

Relating users and admins using a self join

- [Instructor] I'm going to walk you through how to solve this user admin problem using a self join. Self joins can be a little tricky to learn and implement, but this user admin example is a classic and fairly simple business problem to learn self joins. So let's get started. First, let's build the base of our query with select star from users. All right, let's run it. In this table, user ID is the primary key of the table. An admin ID is a foreign key because it references another key, user ID. Now, let's join the user table to itself by joining both tables on user ID. That kind of makes sense, right? Let's see what happens. So we're going to left join a copy of the user's table and we're going to call the second copy of the user's table admin. And we're going to try joining these on user ID. Users dot user ID from the first table and admin dot user ID from the second table. Let's run it and see what we get. And…

内容