MySQL/SQL:使用更新表本身的相关子查询进行更新

发布于 2024-07-18 09:52:31 字数 699 浏览 6 评论 0原文

我有一个通用问题,我将尝试用一个例子来解释。

假设我有一个包含字段的表:“id”,“name”,“category”,“appearances”和“ratio”

这个想法是我有几个项目,每个项目与一个类别相关并且“出现”多次。 比率字段应包括每个项目的出现次数占该类别中项目的出现总数的百分比。

在伪代码中,我需要的是以下内容:

  • 对于每个类别
    找出与其相关的项目出现的总和。 例如,可以使用 (select sum("appearances") from table group by Category)

  • 对于每个项目
    将比率值设置为项目的外观除以上述类别的总和

现在我尝试通过单个更新查询来实现此目的,但似乎无法做到这一点。 我认为我应该做的是:

update Table T    
set T.ratio = T.appearances /   
(    
select sum(S.appearances)    
from Table S    
where S.id = T.id    
)

但是MySQL不接受更新列中的别名T,并且我没有找到其他方法来实现这一点。

有任何想法吗?

I have a generic question that I will try to explain using an example.

Say I have a table with the fields: "id", "name", "category", "appearances" and "ratio"

The idea is that I have several items, each related to a single category and "appears" several times. The ratio field should include the percentage of each item's appearances out of the total number of appearances of items in the category.

In pseudo-code what I need is the following:

  • For each category
    find the total sum of appearances for items related to it. For example it can be done with (select sum("appearances") from table group by category)

  • For each item
    set the ratio value as the item's appearances divided by the sum found for the category above

Now I'm trying to achieve this with a single update query, but can't seem to do it. What I thought I should do is:

update Table T    
set T.ratio = T.appearances /   
(    
select sum(S.appearances)    
from Table S    
where S.id = T.id    
)

But MySQL does not accept the alias T in the update column, and I did not find other ways of achieving this.

Any ideas?

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

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

发布评论

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

评论(4

爺獨霸怡葒院 2024-07-25 09:52:31

根据我收到的两个答案(都不完整,所以我自己写了),我最终所做的如下:

UPDATE Table AS target
INNER JOIN 
(
select category, appearances_sum
from Table T inner join (
    select category as cat, sum(appearances) as appearances_sum
    from Table
    group by cat
) as agg
where T.category  = agg.cat
group by category
) as source
ON target.category = source.category
SET target.probability = target.appearances / source.appearances_sum 

它运行得非常快。 我还尝试了相关子查询,但速度慢得多(数量级),所以我坚持使用连接。

Following the two answers I received (none of which was complete so I wrote my own), what I eventually did is as follows:

UPDATE Table AS target
INNER JOIN 
(
select category, appearances_sum
from Table T inner join (
    select category as cat, sum(appearances) as appearances_sum
    from Table
    group by cat
) as agg
where T.category  = agg.cat
group by category
) as source
ON target.category = source.category
SET target.probability = target.appearances / source.appearances_sum 

It works very quickly. I also tried with correlated subquery but it was much slower (orders of magnitude), so I'm sticking with the join.

给妤﹃绝世温柔 2024-07-25 09:52:31

更新后立即使用连接:
参考手册 – 13.2.11 UPDATE 语法

更新表 1 内连接表 2 ....
设置 table1.foo=值
where table2.bla = someothervalue

对于此类事情,请务必查看手册。 MySql 有适当的参考手册,因此获得正确的语法应该不难;)

Use joins right after UPDATE:
Reference Manual – 13.2.11 UPDATE Syntax

so
UPDATE table1 inner join table2 on ....
set table1.foo=value
where table2.bla = someothervalue

With these kind of things, always look at the manual. MySql has a proper reference manual, so it shouldn't be that hard to get the right syntax ;)

琉璃繁缕 2024-07-25 09:52:31

这是在mssql中完成的,我认为mysql是相同或相似的:

create table T (id int, ratio float, appearances int)
insert T values (1, null, 2)
insert T values (1, null, 3)

update T
set ratio = cast(appearances as float)/ agg.appearancesSum
from T join (
    select id, sum(appearances) as appearancesSum
    from T
    group by id
) as agg on t.id = agg.id

This is how it is done in mssql, I think mysql is the same or similar:

create table T (id int, ratio float, appearances int)
insert T values (1, null, 2)
insert T values (1, null, 3)

update T
set ratio = cast(appearances as float)/ agg.appearancesSum
from T join (
    select id, sum(appearances) as appearancesSum
    from T
    group by id
) as agg on t.id = agg.id
生寂 2024-07-25 09:52:31

这是一个旧的,但我认为人们仍在关注它。 我查看了首选答案,但继续为我的 MariaDB/MySQL 数据库处理相关子查询。 相关子查询是我最不喜欢放在一起的,因为它们在我的脑海中总是有意义的,直到我尝试配置一个。 哈哈,

我尝试了几种相关子查询的风格,它更新了表中的所有记录或所有具有相同公共键的记录。

最终起作用的是在目标表上添加另一个“哪里存在选择”。

我正在更新 2 个加密记录,这些记录是从同一个表上具有有效加密字段的记录中损坏的。 剪切并粘贴到标准更新语句中不起作用。

根据它更新的行数,我运行回滚或提交,直到
我有一个子查询更新了预期的 2 行

start transaction;

update account tar
   set value =  (select value 
                 from account src
                 Where src.name = tar.name
                   and src.name = 'acct_no'
                   and src.customAttributesId = '8adac1537dbe5c39017dc89f082e0341') 
    Where exists (select 1
                  from account
                  where tar.name = 'acct_no'
                   and tar.customAttributesId in 
                 ('8adac1537dbe5c39017dc8a0e5db0610', 
                  '8adac1537dbe5c39017dc8a0e491060d')
                );

Rollback;
Commit;

15:43:16    update account tar    set value =  (select value                   from)    2 row(s) affected Rows matched: 2  Changed: 2  Warnings: 0  0.141 sec

我有另一个进程确认更新字段中的加密帐号是有效的。 希望这可以帮助。

This is an old one but I think people are still looking this over. I looked at the preferred answer, but continued to work the correlated subquery for my MariaDB/MySQL database. Correlated subqueries are my least favorite to put together as they always make sense in my head till I try to configure one. lol

I tried several flavors a correlated subquery that the updated all the records in the table or all with same common key.

What ended up working was adding another "where exists select" on the target table.

I am updating 2 encrypted records that got corrupted from a record on the same table that has a valid encrypted field. A cut and paste into a standard update statement did not work.

Depending on the number of rows it updated I ran the roll back or commit, Until
I got a subquery that updated the expect 2 rows

start transaction;

update account tar
   set value =  (select value 
                 from account src
                 Where src.name = tar.name
                   and src.name = 'acct_no'
                   and src.customAttributesId = '8adac1537dbe5c39017dc89f082e0341') 
    Where exists (select 1
                  from account
                  where tar.name = 'acct_no'
                   and tar.customAttributesId in 
                 ('8adac1537dbe5c39017dc8a0e5db0610', 
                  '8adac1537dbe5c39017dc8a0e491060d')
                );

Rollback;
Commit;

15:43:16    update account tar    set value =  (select value                   from)    2 row(s) affected Rows matched: 2  Changed: 2  Warnings: 0  0.141 sec

I have another process that confirmed the encrypted Account number in the updated field was valid. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文