【问题标题】:How to Convert this sql query into laravel query builder如何将此 sql 查询转换为 laravel 查询生成器
【发布时间】:2017-06-01 08:11:51
【问题描述】:

选择 t.product_id,t.on_hand,t.created_at 来自table t, (SELECT MAX(purchase_id) as pId FROM table Group by product_id) tg 其中 t.purchase_id = tg.pId

【问题讨论】:

  • 你自己尝试过什么吗?
  • 我已经尝试了超过 3 天。谢谢

标签: php mysql laravel laravel-5 eloquent


【解决方案1】:
$result = DB::table('table as t')
->select('t.product_id', 't.on_hand', 't.created_at')
->join(DB::raw('(SELECT MAX(purchase_id) as pId FROM table Group by product_id) tg'), function($join) {
    $join->on('t.purchase_id', '=', 'tg.pId');
})->get();

【讨论】:

  • 感谢此查询在 var_dump $result 但未显示 var_dump 错误时有效。"未定义属性:stdClass::$products (查看:E:\Xampp\htdocs\inventory\resources\views\pages\stock .blade.php)" 这里这个表与“产品”有“关系”。而这种关系是---“购买腰带到产品”----
猜你喜欢
  • 2019-09-08
  • 2021-08-07
  • 2015-12-13
  • 2023-01-14
  • 2021-04-30
  • 1970-01-01
  • 2019-10-14
  • 2019-06-21
  • 1970-01-01
相关资源
最近更新 更多