You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何关联两张10行表的MySQL查询未返回10条结果?

MySQL关联查询未返回预期行数问题排查

我关联了两张各包含10行数据的表,关联字段为唯一列。我清楚CTE表可能存在重复值,但查询结果本应返回10条记录。

对应的SQL查询语句如下:

with ints as (
  select 1 as i
  union all select 2
  union all select 3
  union all select 4
  union all select 5
  union all select 6
  union all select 7
  union all select 8
  union all select 9
  union all select 10
),cte as (
   select floor(rand()*10)+1 as x
   from ints
)
select
   i.i, c1.x
from ints i
inner join cte c1 on c1.x=i.i
order by i.i;

以下是随机生成的查询结果(每次执行结果会变化):

ix
28
25
33
54
88
102

编辑:由于好奇MySQL在未使用LIMIT的情况下未返回10条记录的原因,我提交了编号为111852的bug报告。

编辑(2023-07-28):MySQL官方对该bug的回复:

您可以通过禁用派生表合并来避免此问题。该问题可能是优化器将派生表和视图引用合并到外部查询块时产生的。

解决该问题的命令如下:

set optimizer_switch = 'derived_merge=off';

内容的提问来源于stack exchange,提问作者Luuk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 03:23:19