开发者

Two Inner Joins MYSQL

开发者 https://www.devze.com 2023-02-09 11:59 出处:网络
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

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.开发者_StackOverflow..

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 e.g. 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'
0

精彩评论

暂无评论...
验证码 换一张
取 消