【问题标题】:Laravel 5.2. DB table + Join with Update拉拉维尔 5.2。数据库表 + 加入更新
【发布时间】:2017-03-12 19:08:45
【问题描述】:

我需要表目录中的更新键,我写查询(mysql查询是对的):

update attributes a inner join catalog c on a.parent_id = c.id set a.key = c.left_key

和 Laravel DB:

DB::table('attributes as a')
    ->join(catalog as c', 'a.parent_id', '=', 'c.id')
    ->update([ 'a.key' => 'c.left_key' ]);

这是正确的查询,但是 DB::table ... 将表目录中的所有键都设置为 0。 我写

DB::statement('update attributes a inner join catalog c on a.parent_id = c.id set a.key = c.left_key;');

它的工作!但我不明白为什么带有连接和更新的 DB::table 不起作用

【问题讨论】:

    标签: php mysql laravel inner-join


    【解决方案1】:

    你可以试试:

    DB::table('attributes as a')
       ->join('catalog as c', 'a.parent_id', '=', 'c.id')
       ->update([ 'a.key' => DB::raw("`c`.`left_key`") ]);
    

    【讨论】:

      猜你喜欢
      • 2021-07-12
      • 2017-03-08
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-30
      • 1970-01-01
      • 2016-06-20
      相关资源
      最近更新 更多