两个内连接 MYSQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4916930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Two Inner Joins MYSQL
提问by Yoshiyahu
How would I preform two inner joins in one query?
我将如何在一个查询中执行两个内部联接?
Ie: three tables
即:三个表
Invoice
Address
Client
发票
地址
客户端
Invoice has a column which references an id in clients. It also has a column which references an address. I need to get both the clients name from the matched table and the address from the matched table. How would I INNER JOIN
both tables?
Invoice 有一列引用客户中的 id。它还有一个引用地址的列。我需要从匹配表中获取客户端名称和匹配表中的地址。我怎么会INNER JOIN
两个表?
I'll add a few details...
invoice has rows address(references address id), client(references client id), id and notes
client has rows first_name, last_name
address has rows street_name and city
我将添加一些详细信息...
发票有行地址(引用地址 ID),客户(引用客户 ID),id 和注释 客户有行 first_name,last_name 地址有行 street_name 和 city
I need to pull up
我需要拉起来
回答by Dan Grossman
You can have as many JOIN clauses as you need in the query. Each has an ON clause where you specify the related columns between the joined tables.
您可以根据需要在查询中使用任意数量的 JOIN 子句。每个都有一个 ON 子句,您可以在其中指定连接表之间的相关列。
SELECT
columns
FROM
invoice
INNER JOIN
address
ON
join_condition
INNER JOIN
client
ON
join_condition
回答by Prisoner
Something like:
就像是:
SELECT
c.*, i.*, a.*
FROM
invoices i
INNER JOIN
client c
ON
i.clientid = c.clientid
INNER JOIN
address a
ON
a.clientid = c.clientid
WHERE
i.id = 21
Don't forget you only select the fields you require, not * (all).
不要忘记您只选择您需要的字段,而不是 *(全部)。