使用union查询结果不符合预期

  • 【TiDB 版本】:3.0.7
  • 【问题描述】:

执行sql语句:

select from_unixtime(a.created_time,’%Y-%m-%d’) from yop_user a where a.username = ‘test’ UNION select from_unixtime(a.created_time,’%Y-%m-%d’) from yop_user a where a.username = ‘test’;

麻烦加以描述,如果和预期不符合,麻烦提供下面 union 操作拆解为 2 个 SQL 结果验证是否符合预期。

两条sql是一样的,截图中第一条是单条sql执行结果

麻烦发一下两次 SQL 分别结果截图,以及 explain analyze 截图。

select from_unixtime(a.created_time,’%Y-%m-%d’) from yop_user a where a.username = ‘test’ UNION select from_unixtime(a.created_time,’%Y-%m-%d’) from yop_user a where a.username = ‘test’;

select from_unixtime(a.created_time,’%Y-%m-%d’) from yop_user a where a.username = ‘test’ 


explain analyze select from_unixtime(a.created_time,’%Y-%m-%d’) from yop_user a where a.username = ‘test’ UNION select from_unixtime(a.created_time,’%Y-%m-%d’) from yop_user a where a.username = ‘test’;

explain select from_unixtime(a.created_time,’%Y-%m-%d’) from yop_user a where a.username = ‘test’ 

单条sql执行结果和explain详情:

union语句执行结果和explain详情:

稍等,确认一下