Mysql -- UPDATE table SET column = SELECT COUNT(*) FROM ( SELECT * FROM table2 WHERE table2.id = table.id ) ) Impossible(Mysql -- UPDATE table SET column = SELECT COUNT(*) FROM (SELECT * FROM table2 WHERE table2.id = table.id)) 不可能)
问题描述
我有两个包含很多行的表,我需要为第一个 (table_1) 中的每一行维护索引"信息.所以我写了一个查询不直接使用 COUNT() [这是慢,慢,慢].所以我尝试:
<上一页>更新 table_1 SET table_1.column_3 = (选择计数(*)从(SELECT DISTINCT column_5 FROM table_2 WHERE table_2.id_t1 = table_1.id LIMIT 300) 吨)但是 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))
I have two tables with a lot of rows and I need to maintain "index" informations for each row in the first (table_1). So I wrote a query to not use directly COUNT() [wich is slow, slow, slow]. 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)
Did you know how to passs table_1.id in the where clause ? 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:
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:
IF(count(distinct column_5)>300, 300, count(distinct column_5))
这篇关于Mysql -- UPDATE table SET column = 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)) 不可能
基础教程推荐
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
