SQL 中交换一对多关系方向的最佳方法是什么?

发布于 2024-09-18 17:35:44 字数 1096 浏览 5 评论 0原文

好吧,不管出于什么原因,我最终遇到了这样一种情况:关键是在一对多中指向错误的方向。显然它从来没有用作一对多,只用作一对一,现在需要将其中一个扩展为多个,而密钥的存储方式结果是倒退的。

images 表具有 target_idtarget_typetarget_column 这三个信息,可通过任意数量的内容表。 target_type 仅引用与图像关联的内容片段的表名称。 target_column 是用于查找图像的虚拟列(实际上不在内容表中)的名称。这使得任意内容片段都可以具有多个关联图像,每个图像具有不同的名称。

当您拥有一段内容并想要查找与特定名称相关联的图像时,您可以执行以下操作:

select * from images where target_id = content.id 
    and target_type = "content" 
    and target_column = "image";

当您引用特定内容时,所有这些信息都可用。

我想做的是反转所有这些,以便图像表对引用它的特定内容片段一无所知,并且该负担由每个内容表承担。

到目前为止,我知道我可以向内容表添加一列,然后从图像表中选择我需要的信息:

select id, target_id from images where target_type = "content";

我想要做的是将其用作子查询并对内容表进行批量更新。这样的事情可能吗?

update content set image_id = 
    (select id from images where target_type = "content") as image_ids where id =
    (select target_id from images where target_type = "content") as content_ids;

我知道这会失败,但我想对 target_ids 进行某种批量分配回 image_ids 。这是疯狂吗?我该怎么做?

Okay, so for whatever reason I have ended up with a situation where the key is pointing the wrong way in a one-to-many. It was obviously never used as a one-to-many, only as a one-to-one, and now there is a need to expand one of those to be many, and the way the key was stored this turned out to be backwards.

The images table has a target_id, target_type and target_column, three pieces of information which identify it with any number of content tables. The target_type just references the table name of the piece of content that is associated to the image. target_column is the name of a virtual column (not actually in the content table) that is used to look up the image. This enables any arbitrary piece of content to have several associated images, each by a different name.

When you have a piece of content and want to find what image is associated to a particular name, you do a

select * from images where target_id = content.id 
    and target_type = "content" 
    and target_column = "image";

All of these pieces of information are available when you have a reference to a particular piece of content.

What I want to do instead is REVERSE all of these, so that the images table knows nothing about the particular pieces of content that reference it, and instead that burden is carried by each of the content tables.

So far I know I can add a column to the content table and then select the information I need from the images table:

select id, target_id from images where target_type = "content";

What I want to do is use this as a subquery and do a mass update of the content table. Is something like this possible?

update content set image_id = 
    (select id from images where target_type = "content") as image_ids where id =
    (select target_id from images where target_type = "content") as content_ids;

I know this fails, but I want to do some kind of mass assignment of target_ids back to image_ids. Is this madness? How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝海 2024-09-25 17:35:44

你可能想使用Mysql的多表更新机制。 (参见http://dev.mysql.com/doc/refman/ 5.0/en/update.html

在你的情况下,这就是

update
    content,
    images
set
    content.image_id = images.id
where
    images.target_id = content.id
    and images.target_type = 'content'
    and images.target_column = 'images'

我希望这会对你有所帮助

Jerome Wagner

you might want to use the Mysql multiple-table update mechanism. (cf http://dev.mysql.com/doc/refman/5.0/en/update.html)

in your case, this would be

update
    content,
    images
set
    content.image_id = images.id
where
    images.target_id = content.id
    and images.target_type = 'content'
    and images.target_column = 'images'

I hope this will help you

Jerome Wagner

一笑百媚生 2024-09-25 17:35:44

您可以加入 images 表来进行更新:

update content inner join images on images.target_id = content.id and 
    images.target_type = 'content' and images.target_column = 'images'
set content.image_id = images.id

You can join in the images table to do the update:

update content inner join images on images.target_id = content.id and 
    images.target_type = 'content' and images.target_column = 'images'
set content.image_id = images.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文