L
L
Lulzsec2016-06-25 18:42:54
PHP
Lulzsec, 2016-06-25 18:42:54

How to correctly compose an SQL query (JOIN)?

There are 3 tables:
1) Main table with orders
fddeb204e5a847449a5d0074d1152bb0.png
2) Connecting table
58e7e5f9da0143aebc4b80dbd1cb6345.png
3) Main table with goods
d4afc01c0c9f4690a468877437608a51.png
I fight with JOINs, it doesn't work. How to draw columns: product name (from 3 tables), quantity (1 table), cost (1 table)?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DTX, 2016-06-25
@Lulzsec

SELECT
  a.price, 
  a.count,
  c.name
FROM
  a
  LEFT JOIN b ON a.id_order = b.id_order
  LEFT JOIN c ON b.id_product = c.id_product

N
nozzy, 2016-06-25
@nozzy

select
t3.name,
t1.count,
t1.price
from table1 t1
inner join table2 t2 on t2.id_order = t1.id_order
inner join table3 t3 on t3.id_product = t2.id_product

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question