简体   繁体   中英

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. 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?

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

I need to pull up

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.

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:

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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