How to write an UPDATE Query for multiple tables in Common Table Expressions (CTE's) format?

守給你的承諾、 提交于 2021-02-11 16:52:42

问题


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');

回答1:


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


来源:https://stackoverflow.com/questions/60416996/how-to-write-an-update-query-for-multiple-tables-in-common-table-expressions-ct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!