更新总字段

发布于 2024-09-03 00:14:50 字数 388 浏览 9 评论 0原文

我在更新表时遇到问题。遵循表结构:

Table1
  tableid
  ...
  ... 
  productID_1
  productID_2
  productID_3

Table2
  productID
  Total

我必须汇总表2中的每个产品。

例如:

SELECT COUNT(*) as tot, ProductID_1 FROM Table1 GROUP Table1 

然后 UPDATE table2 SET Total =..??? (我该怎么办)WHERE ProductID_1 = ....

希望你能帮助我。

谢谢

i've a problem for a table update. follow table structure:

Table1
  tableid
  ...
  ... 
  productID_1
  productID_2
  productID_3

Table2
  productID
  Total

I've to totalize each product in table2.

For example:

SELECT COUNT(*) as tot, ProductID_1 FROM Table1 GROUP Table1 

then the UPDATE table2 SET total =..??? (how can i do) WHERE productID_1 = ....

Hope you can help me.

Thank you

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

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

发布评论

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

评论(3

稍尽春風 2024-09-10 00:14:50

您在简化查询方面的选择很大程度上取决于您所使用的产品和版本。然而,适用于大多数数据库的解决方案是:

Update Table2
Set Total = (
            Select Count(*)
            From (
                    Select productId_1 As ProductId From Table1
                    Union All Select productId_2 From Table1
                    Union All Select productId_3 From Table1
                    ) As Z
            Where Table2.ProductId = Z.ProductId
            Group By ProductId
            )

该查询繁琐的一个重要原因是 Table1 中的数据未标准化。相反,您应该考虑 Table1 的结构,例如:

Create Table Table1 (
                    TableId <datatype> not null
                    , ProductId <datatype> not null
                    , Constraint PK_Table1 Primary Key ( TableId, ProductId )
                    )

Your options in terms of simplifying the query greatly depend on the product and version you are using. However, a solution that should work in most databases would be:

Update Table2
Set Total = (
            Select Count(*)
            From (
                    Select productId_1 As ProductId From Table1
                    Union All Select productId_2 From Table1
                    Union All Select productId_3 From Table1
                    ) As Z
            Where Table2.ProductId = Z.ProductId
            Group By ProductId
            )

A big reason this query is cumbersome is that the data in Table1 is not normalized. Instead you should consider a structure for Table1 like:

Create Table Table1 (
                    TableId <datatype> not null
                    , ProductId <datatype> not null
                    , Constraint PK_Table1 Primary Key ( TableId, ProductId )
                    )
兲鉂ぱ嘚淚 2024-09-10 00:14:50

您可以将第一个结果存储在临时表/表变量中(如果您使用的数据库支持)。例如,在 SQL Server 中,您可以执行以下操作:

declare @t table
(
   key int,
   cnt int
)

insert into @t (key, cnt)
select count(*) as tot, ProductID_1 from Table1 ...

如果 ProductID_2 和 ProductID_3 位于同一个表中,则可以合并结果。

然后,插入表2:

insert into table2 (productID, Count)
select key, cnt from @t

You can store the first results in a temp table/table variable (if the DB you are using supports it). For instance, in SQL Server, you can do:

declare @t table
(
   key int,
   cnt int
)

insert into @t (key, cnt)
select count(*) as tot, ProductID_1 from Table1 ...

If ProductID_2 and ProductID_3 are in the same table, you can union the results.

Then, insert into table 2:

insert into table2 (productID, Count)
select key, cnt from @t
不交电费瞎发啥光 2024-09-10 00:14:50
REPLACE INTO table2
SELECT COUNT(*) as total, ProductID 
FROM Table1 
GROUP Table1.ProductID
REPLACE INTO table2
SELECT COUNT(*) as total, ProductID 
FROM Table1 
GROUP Table1.ProductID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文