2017-12-05 50 views
1

2つのテーブルproduct_priceとtrade_channelsを結合する必要がありました。内部結合を使用する場合、
のproduct_priceのIDは削除されています。内部laravelに参加していませんP

ここ

が私のコードである

DB::table('product_price')->where('product_id',$id)->where('action','customer_price') 
     ->join('customers','product_price.action_id','=','customers.id') 
     ->get(); 

答えて

2

良い練習は、あなたが実際にすべての列の必要性を全く使用しないようにしたい列を選択することで役立つことを願います。

あなたは、あなたがこのような何かを行うことができますcustomer_priceテーブルからPRODUCT_PRICEテーブルとだけ顧客IDのすべての列を必要とし、この場合の仮定:あなたは、任意の列を選択することができます

DB::table('product_price') 
->select(['product_price.*','customer_price.id AS customer_id']) 
->where('product_price.product_id',$id) 
->where('product_price.action','customer_price') 
->join('customers','product_price.action_id','=','customers.id') 
->get(); 

をしかし、それは参加のエイリアスを取るために良いことですこの場合のテーブルの列はcustomer_priceなので、両方の表に同じ名前の列があると混乱することはありません。

幸運

+0

これは私のための知識を追加しました –

1

PRODUCT_PRICE idはちょうど、他の名前でPRODUCT_PRICEのIDをプリントアウトして、顧客IDに置き換えるだろう

DB::table('product_price') 
->select('*','product_price.id AS product_price_id') 
->where('product_id',$id) 
->where('action','customer_price') 
->join('customers','product_price.action_id','=','customers.id') 
->get(); 

を試してみてください。 は、それが

関連する問題