我有两个表,有很多行,我需要维护第一行(table_1)中每一行的"索引"信息。所以我写了一个查询,不直接使用 COUNT() [慢,慢,慢]。所以我尝试:
更新table_1设置table_1.列_3 = ( 从 ( 从table_2中选择不同的column_5,其中 table_2.ID_T1 = table_1.ID 限制 300 ) t)
但是MySQL回答我说table_1.id在where子句中是未知的(#1054)
你知道如何在 where 子句中传递 table_1.id 吗?还是其他方式来实现我的目标?
谢谢你帮助我!
问题是因为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);
正如我看到您正在使用 LIMIT,不确定您是否需要它,无论如何您可以模拟它:
IF(count(distinct column_5)>300, 300, count(distinct column_5))