如何使用3个表交叉连接?

时间:2023-01-04 01:01:17

如何使用3个表交叉连接?

i have 3 tables: customer,Barang and Det_Barang.

我有3张桌子:顾客,Barang和Det_Barang。

customer + barang = one to many

客户+ barang =一对多

barang + det_barang = many to many

barang + det_barang =很多很多

How to cross from the 3 table above? And the result as shown in the 4th table

如何从上面的3个表中穿过?结果如第4表所示

Link SQL File : Test.sql

链接SQL文件:Test.sql

2 个解决方案

#1


0  

SELECT c.Nama AS Customer, b.No AS No_Barang, d.Qty
FROM customer c
JOIN barang b
LEFT JOIN det_barang d ON c.No = d.No_Cust AND b.No = d.No_Barang
ORDER BY c.No, b.No

#2


0  

This is the query you're looking for:

这是您正在寻找的查询:

SELECT c.nama
    ,b.no
    ,d.qty
FROM customer c
CROSS JOIN barang b
LEFT JOIN det_barang d ON c.no = d.no
    AND b.no = d.no_barang
ORDER BY c.nama, b.no;

You'll have to adjust the column names to match your table schema.

您必须调整列名称以匹配表架构。

Also, here is a SQLFiddle with the result.

此外,这是一个结果的SQLFiddle。

#1


0  

SELECT c.Nama AS Customer, b.No AS No_Barang, d.Qty
FROM customer c
JOIN barang b
LEFT JOIN det_barang d ON c.No = d.No_Cust AND b.No = d.No_Barang
ORDER BY c.No, b.No

#2


0  

This is the query you're looking for:

这是您正在寻找的查询:

SELECT c.nama
    ,b.no
    ,d.qty
FROM customer c
CROSS JOIN barang b
LEFT JOIN det_barang d ON c.no = d.no
    AND b.no = d.no_barang
ORDER BY c.nama, b.no;

You'll have to adjust the column names to match your table schema.

您必须调整列名称以匹配表架构。

Also, here is a SQLFiddle with the result.

此外,这是一个结果的SQLFiddle。