在 SQL 中更新父属性值

发布于 2024-09-15 04:45:47 字数 407 浏览 4 评论 0原文

我有一个包含 2 个表的 SQL Server 2008 数据库。这些表的定义如下:

Device
------
ID
Name
Description
TotalApplications

Application
-----------
ID
Name
DeviceID

我最近添加了“TotalApplications”列,以加快耗时太长的查询。与设备关联的应用程序数量不会经常变化。当它发生时,我需要编写一个触发器来处理这个问题。但现在,我只需要填充我的数据库。

目前,与每个设备关联的 TotalApplications 为 0。显然,我需要为所有设备记录更新此值。但是,我不知道该怎么做。有人可以解释一下如何更新我的设备记录的 TotalApplications 值吗?

非常感谢您的帮助!

I have a SQL Server 2008 database with 2 tables. These tables are defined like this:

Device
------
ID
Name
Description
TotalApplications

Application
-----------
ID
Name
DeviceID

I recently added the "TotalApplications" column in an effort to speed up a query that is taking WAY too long. The number of applications associated with a device will not change very often. When it does, I will need to write a trigger to handle this. But for now, I just need to populate my database.

Currently, the TotalApplications associated with each device is 0. Clearly I need to update this value for all of my device records. However, I cannot figure out how to do this. Can someone please explain how I can update the TotalApplications value for my Device records?

Thank you SO much for your help!

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

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

发布评论

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

评论(2

烈酒灼喉 2024-09-22 04:45:56

OMG Ponies 答案肯定适用于此,但如果这是我的数据库,我将避免添加列并使用触发器。相反,使用索引,否则我无法使用索引到达那里,我会使用索引视图。如果您希望我发布该内容,请告诉我。

OMG Ponies answer would certainly work for this, but if this were my db I would avoid doing adding the column and using the trigger. Instead use indexing or I cant get there with indexes I would use an indexed view. Let me know if you want me to post that.

情深缘浅 2024-09-22 04:45:54

使用:

UPDATE DEVICE
   SET totalapplications = (SELECT COUNT(*)
                              FROM APPLICATION a
                             WHERE a.deviceid = DEVICE.id)
 WHERE totalapplications = 0

Use:

UPDATE DEVICE
   SET totalapplications = (SELECT COUNT(*)
                              FROM APPLICATION a
                             WHERE a.deviceid = DEVICE.id)
 WHERE totalapplications = 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文