gpt4 book ai didi

mysql - WHERE 中的 SQL 子查询返回有限的结果集,但仍匹配所有结果

转载 作者:行者123 更新时间:2023-11-29 13:17:57 27 4
gpt4 key购买 nike

我需要 SQL 查询方面的帮助:

UPDATE content_type_blog b
LEFT JOIN node n
ON n.nid = b.nid
SET b.field_is_latest_value = 1
WHERE n.nid IN (
(
SELECT nid
FROM node
GROUP BY uid
ORDER BY created DESC
)
);

WHERE 子句中的 select 返回 4012 个结果,但是当整个查询运行时,它会更新 124k 个结果,而不是将其限制为子查询中匹配的 4012 个结果。

此查询旨在更新 content_type_blog - 按 nid 的表列,其中创建的列在节点表中最高(最新)。

最佳答案

您的子查询过滤掉重复的 nid 值,但它仍然返回所有这些值。所以它根本不是一个过滤器。

在其他数据库中,您所要求的通常是通过像 row_number() 这样的窗口函数来完成的。但 MySQL 不支持这一点。相反,您可以使用过滤联接。

这是一个例子。 SQL Fiddle 似乎出现故障,因此我无法对其进行测试,但我希望它能为您指明正确的方向。

create table table1 (col1 int, col2 bit, created datetime)
insert table1 values
(1, 0, '2013-01-01'),
(1, 0, '2013-01-02'),
(1, 0, '2013-01-03'),
(2, 0, '2013-01-01');

update table1
join (
select col1
, max(created) as max_created
from table1
group by
col1
) filter
on filter.col1 = table1.col1
and filter.max_created = table1.creatd
set col2 = 1

select *
from table1

关于mysql - WHERE 中的 SQL 子查询返回有限的结果集,但仍匹配所有结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21230914/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com