[英]MySql Join more than 2 tables?
I have the following 3 tables and want to get into the "inventory" table where user_id = user_id and want to show all "ownedskins" from a user with this id. 我有以下3个表,并希望进入“清单”表,其中user_id = user_id,并希望显示具有该ID的用户的所有“所有皮肤”。
How can I do this? 我怎样才能做到这一点?
Thanks for all replys! 感谢您的所有回复!
Joining more than 2 tables with MySql example: 使用MySql示例连接两个以上的表:
SELECT i.*
FROM ownedskins o
JOIN inventory i
ON o.inventory_id=i.inventory_id
JOIN user u
ON u.user_id = i.user_id
WHERE u.user_id = 1
Using joins is very easy, use something like this: 使用联接非常容易,请使用以下方法:
SELECT *
FROM table1 t1
JOIN table2 t2 ON t1.id = t2.id
JOIN table3 t3 ON t2.id2 = t3.id
You can also use JOIN variants like LEFT JOIN, INNER JOIN, OUTER JOIN... 您还可以使用JOIN变体,例如LEFT JOIN,INNER JOIN,OUTER JOIN ...
Let's say you have the following tables: 假设您有以下表格:
Driver
Car
Keys
If you want access to all 3 of them you could do the following: 如果您想访问全部三个,则可以执行以下操作:
SELECT D.name, C.plate, K.state
FROM Driver D, Car C, Keys K
WHERE C.key_id = K.id and
K.owner_id = D.id;
Using JOINS: 使用JOINS:
SELECT D.name, C.plate, K.state
FROM Car C JOIN Keys K
ON C.key_id = K.id JOIN Driver D
ON K.owner_id = D.id;
Given that you know the user_id
, you could do that with a single join
, like this: 假设您知道
user_id
,则可以通过单个join
来做到这一点,如下所示:
SELECT
ownedskins.*
FROM
ownedskins JOIN inventory ON ownedskins.inventory_id = inventory.inventory_id
WHERE
inventory.user_id = x
Where x
is the id of the user you want 其中
x
是您想要的用户的ID
You can also do it with alias to make it look cleaner: 您也可以使用别名使其看起来更整洁:
SELECT
os.*
FROM
ownedskins os JOIN inventory i ON os.inventory_id = i.inventory_id
WHERE
i.user_id = x
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.