laravel 拉拉维尔 5.2。数据库表 + 加入更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40329670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.2. DB table + Join with Update
提问by Misha Babich
I need update key in table catalog and I write query (mysql query is right):
我需要表目录中的更新键并编写查询(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;');
```
And it's work! But I don't understand why DB::table with join and update don't work
这是工作!但我不明白为什么 DB::table with join 和 update 不起作用
回答by Amit Gupta
You can try as:
您可以尝试如下:
DB::table('attributes as a')
->join('catalog as c', 'a.parent_id', '=', 'c.id')
->update([ 'a.key' => DB::raw("`c`.`left_key`") ]);
回答by Glenys Mitchell
Probably the number of characters in the two columns are not the same.
可能两列中的字符数不一样。
make sure that the two assemblies have the same formatting or size
确保两个程序集具有相同的格式或大小