Laravel 5.2。数据库表+加入更新

时间:2016-10-29 14:31:44

标签: php mysql laravel inner-join

我需要表目录中的更新密钥,我编写查询(mysql查询是正确的):

```
update attributes a inner join catalog c on a.parent_id = c.id set a.key = c.left_key
```
and with Laravel DB:
```
DB::table('attributes as a')
    ->join(catalog as c', 'a.parent_id', '=', 'c.id')
    ->update([ 'a.key' => 'c.left_key' ]);
```
It's right query, but DB::table ... make all key in table catalog to 0.
I write 
```
DB::statement('update attributes a inner join catalog c on a.parent_id = c.id set a.key = c.left_key;');
```

它的工作!但我不明白为什么DB :: table with join and update不能正常工作

2 个答案:

答案 0 :(得分:2)

您可以尝试:

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

答案 1 :(得分:-1)

两列中的字符数可能不相同。

确保两个程序集具有相同的格式或大小