内部加入 laravel 缺少 P
Inner Join in laravel missing P
我需要结合两个 table product_price 和 trade_channels,当我使用内部连接时,
product_price 的 ID 已删除。
这是我的代码
DB::table('product_price')->where('product_id',$id)->where('action','customer_price')
->join('customers','product_price.action_id','=','customers.id')
->get();
尝试
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();
product_price id 将替换为 customers id,因此只需使用其他名称打印出 product_price id。
希望对你有帮助
一个好的做法是 select 一个您实际想要使用的列而不需要所有列。
假设在这种情况下您需要 product_price table 的所有列并且只需要来自 customer_price table 的客户 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();
您可以 select 任何列,但最好使用 join table 列的别名,在这种情况下它是 customer_price 所以如果两个 table 都不会混淆具有相同名称的列。
祝你好运
我需要结合两个 table product_price 和 trade_channels,当我使用内部连接时,
product_price 的 ID 已删除。
这是我的代码
DB::table('product_price')->where('product_id',$id)->where('action','customer_price')
->join('customers','product_price.action_id','=','customers.id')
->get();
尝试
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();
product_price id 将替换为 customers id,因此只需使用其他名称打印出 product_price id。 希望对你有帮助
一个好的做法是 select 一个您实际想要使用的列而不需要所有列。
假设在这种情况下您需要 product_price table 的所有列并且只需要来自 customer_price table 的客户 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();
您可以 select 任何列,但最好使用 join table 列的别名,在这种情况下它是 customer_price 所以如果两个 table 都不会混淆具有相同名称的列。
祝你好运