简体   繁体   English

Mysql - UPDATE表SET列= SELECT COUNT(*)FROM(SELECT * FROM table2 WHERE table2.id = table.id))不可能

[英]Mysql — UPDATE table SET column = SELECT COUNT(*) FROM ( SELECT * FROM table2 WHERE table2.id = table.id ) ) Impossible

I have two tables with a lot of rows and I need to maintain "index" informations for each row in the first (table_1). 我有两个包含大量行的表,我需要为第一行(table_1)中的每一行维护“索引”信息。 So I wrote a query to not use directly COUNT() [wich is slow, slow, slow]. 所以我写了一个查询,不直接使用COUNT()[慢,慢,慢]。 So I try : 所以我尝试:

UPDATE table_1 SET table_1.column_3 = (  
    SELECT COUNT(*) FROM (
        SELECT DISTINCT column_5 FROM table_2 WHERE table_2.id_t1 = table_1.id LIMIT 300
    ) t
)

But MySQL answering me that table_1.id is unknow in where clause (#1054) 但MySQL回答我,table_1.id在where子句中是未知的(#1054)

Did you know how to passs table_1.id in the where clause ? 你知道如何在where子句中传递table_1.id吗? Or other way to get my goal ? 或者其他方式来实现我的目标?

Thank you for helping me ! 感谢你们对我的帮助 !

the problem is because table_1 is too far from inner query, use: 问题是因为table_1离内部查询太远,使用:

UPDATE table_1 SET table_1.column_3 = 
(SELECT count(DISTINCT column_5) FROM table_2 WHERE table_2.id_t1 = table_1.id);

as I see you're using LIMIT, not really sure that you need it, anyway you can emulate it: 因为我看到你正在使用LIMIT,不确定你是否需要它,无论如何你可以模仿它:

IF(count(distinct column_5)>300, 300, count(distinct column_5))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM