【问题标题】:Laravel join query with two column in same tableLaravel连接查询与同一张表中的两列
【发布时间】:2019-01-10 11:00:35
【问题描述】:

我的代码

$result=DB::table('receipts')
      ->join('dealer_m','receipts.ToDealerID','dealer_m.DealerID')
      ->join('dealer_m','receipts.FromDealerID','dealer_m.DealerID')
      ->join('product_m','receipts.ProductID','product_m.ProductID')->get();

餐桌收据

Id | FromDealerId  | ToDealerId  |  ProductId
---+---------------+-------------+------------
1         1              2            1

2         1              3            1

3         3              1            1

餐桌经销商_m

DealerId | DealerName    
---------+-----------
  1         Dealer One
  2         Dealer Two
  3         Dealer Three

表product_m

ProductId | ProductName
----------+-----------
    1       Product One
    2       Product Two

预期输出

Id | FromDealerId  | ToDealerId  |  ProductId | FromDealerName | ToDealerName | ProductName
---+---------------+-------------+------------+----------------+--------------+------------


 1         1              2            1         Dealer One      Dealer Two  Product One

 2         1              3            1         Dealer One      Dealer Three  Product One

 3         3              1            1         Dealer Three    Dealer One   Product One

当我运行我的代码时,它得到一个错误

SQLSTATE[42000]:语法错误或访问冲突:1066 不唯一 表/别名:'dealer_m'(SQL:从'receipts'内部连接中选择'' 'receipts' 上的'dealer_m'。'ToDealerID' = `dealer_m'.'DealerID' 内部 在 'receipts' 上加入 'dealer_m'。'FromDealerID' = 'dealer_m'.'DealerID' 'receipts' 上的内部连接 ​​'product_m'。'ProductID' = 'product_m'.'ProductID')

这个怎么解决???

谢谢

【问题讨论】:

  • 您需要为连接设置别名。以join('dealer_m AS dealer1') 为例。

标签: mysql laravel join


【解决方案1】:

试试这个:

$results=DB::table('receipts')
      ->select('receipts.*','dealer_m1.DealerName as ToDealer','dealer_m2.DealerName as FromDealer','product_m.ProductName')
      ->join('dealer_m as dealer1','receipts.ToDealerID','dealer_m.DealerID')
      ->join('dealer_m as dealer2','receipts.FromDealerID','dealer_m.DealerID')
      ->join('product_m','receipts.ProductID','product_m.ProductID')->get();

你只需要给每个连接的表一个别名

现在在你的刀片中:

@foreach ($results as $result)
  <tr>
    <td>{{$result->Id}}</td>
    <td>{{$result->FromDealerId}}</td>
    <td>{{$result->ToDealerId}}</td>
    <td>{{$result->ProductId}}</td>
    <td>{{$result->FromDealer}}</td>
    <td>{{$result->ToDealer}}</td>
    <td>{{$result->ProductName}}</td>
  </tr>
@endforeach

当然,如果您对在刀片中调用什么内容感到困惑,只需打印我们的集合以查找键 => 值或执行 dd($results);在控制器进入视图之前对其进行检查。

【讨论】:

  • 但是如何获取 FromDealerName 和 ToDealerName。看看我的预期输出
  • 更新了我的答案
【解决方案2】:

你应该试试这个:

$result=DB::table('receipts')
      ->join('dealer_m as dealer_id','receipts.ToDealerID','dealer_m.DealerID')
      ->join('dealer_m as dealer_f_id','receipts.FromDealerID','dealer_m.DealerID')
      ->join('dealer_m as product_p_id','receipts.ProductID','product_m.ProductID')->get();

【讨论】:

  • 但是如何获取 FromDealerName 和 ToDealerName。看看我的预期输出
猜你喜欢
  • 1970-01-01
  • 2020-11-05
  • 1970-01-01
  • 2013-07-19
  • 1970-01-01
  • 1970-01-01
  • 2014-08-17
  • 2017-06-13
  • 1970-01-01
相关资源
最近更新 更多