Ad

Complex Sql Join Table

- 1 answer

I have 4 table
chart, chart_detail, customer, produk
like this

enter image description hereenter image description here

enter image description hereenter image description here

I wanna run sql comand to list data in chart table

I don't have problem to join cart table with chart detail & customer table because

chart.id = cha>enter image description here

I wanna run sql comand to list data in chart table

I don't have problem to join cart table with chart detail & customer table because

chart.id = chart_detail.id_chart
chart.id_customer = customer.id

DB::table('chart')
    ->join('customer', 'chart.id_customer', '=', 'customer.id')
    ->join('chart_detail', 'chart.id', '=', 'chart_detail.id_chart')->get(); 

but i have problem can't access nama_produk
( in chart_detail table there is only id_produk)...so i need to join chart table with produk table

DB::table('chart')
    ->join('customer', 'chart.id_customer', '=', 'customer.id')
    ->join('produk', 'produk.id', '=', 'chart_detail.id_produk')
    ->join('chart_detail', 'chart.id', '=', 'chart_detail.id_chart')->get(); 

but i get error like this

Column not found: 1054 Unknown column 'chart_detail.id_produk' in 'on clause'

because in chart table there is not available id_produk
id_produk available in chart_detail table

i wonder how to solve it

Ad

Answer

You had your joins out of order, hence the error about the unknown column. Try the following code:

DB::table('chart')
    ->join('customer', 'chart.id_customer', '=', 'customer.id')
    ->join('chart_detail', 'chart.id', '=', 'chart_detail.id_chart')
    ->join('produk', 'produk.id', '=', 'chart_detail.id_produk')->get(); 
Ad
source: stackoverflow.com
Ad