如何编写通用表表达式(CTE)格式的多个表的UPDATE查询? [英] How to write an UPDATE Query for multiple tables in Common Table Expressions (CTE's) format?
本文介绍了如何编写通用表表达式(CTE)格式的多个表的UPDATE查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何将以下UPDATE语句重写为通用表表达式(CTE)格式?
Ho do I rewrite the following UPDATE statement into a Common Table Expressions (CTE's) format?
DB::statement('UPDATE taggables, threads SET taggables.created_at = threads.created_at, taggables.updated_at = threads.updated_at WHERE taggables.thread_id = threads.id');
推荐答案
我在这里看不到使用CTE的意义.由于似乎您的目的是从 threads
更新 taggables
,所以我建议您仅使用
I don't really see the point for using a CTE here. Since it seems like your purpose is to update taggables
from threads
, I would suggest simply using Postgres' update/join syntax, which goes like:
update taggables ta
set created_at = th.created_at, updated_at = th.updated_at
from threads th
where ta.thread_id = th.id
这篇关于如何编写通用表表达式(CTE)格式的多个表的UPDATE查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文