简体   繁体   English

两个内部连接 MYSQL

[英]Two Inner Joins MYSQL

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.发票有一列引用客户中的 id。 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?我将如何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我需要拉起来

You can have as many JOIN clauses as you need in the query. 您可以在查询中拥有所需数量的JOIN子句。 Each has an ON clause where you specify the related columns between the joined tables. 每个子句都有一个ON子句,您可以在其中指定连接表之间的相关列。

SELECT
  columns
FROM
  invoice
INNER JOIN
  address
ON
  join_condition
INNER JOIN
  client
ON
  join_condition

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). 别忘了你只选择你需要的字段,而不是*(全部)。

A sample eg based on learning from @Dan Grossman above:基于从上面的@Dan Grossman学习的示例例如:

SELECT FILM.TITLE, ACTOR.FIRST_NAME, ACTOR.LAST_NAME FROM ACTOR
INNER JOIN FILM_ACTOR
ON ACTOR.ACTOR_ID = FILM_ACTOR.ACTOR_ID
INNER JOIN FILM
ON FILM_ACTOR.FILM_ID = FILM.FILM_ID
WHERE ACTOR.FIRST_NAME = 'Nick' AND ACTOR.LAST_NAME = 'Wahlberg'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM