今天碰到一个关于exists很奇怪的问题
第一个语句如下:
SELECT count(1) FROM APPLY t WHERE EXISTS ( SELECT r.APPLY_ID FROM RECORD r WHERE t.APPLY_ID = r.APPLY_ID );
产生的结果是:89584
第二个语句如下:
SELECT count(1) FROM APPLY t WHERE EXISTS ( SELECT max(r.FINISH_TIME) FROM RECORD r WHERE t.APPLY_ID = r.APPLY_ID );
产生的结果是:432382
确实相当奇怪,对于exist子句来说,其判断的是子查询的值是否存在,也就是说,列名,和对列名求最大值没什么区别啊。
包括MySQL官方文档中也提到
Traditionally, an EXISTS subquery starts with SELECT *, but it could begin with SELECT 5 or SELECT column1 or anything at all. MySQL ignores the SELECT list in such a subquery, so it makes no difference.
大意就是MySQL会自动忽略到SELECT的列表。
后来在自己的环境测试了一下,确实是MySQL的一个bug
测试环境:MySQL 5.6.31,5.7.14
mysql> create table t3(id int,t datetime); Query OK, 0 rows affected (0.44 sec) mysql> insert into t3 values(1,'20160812'); Query OK, 1 row affected (0.16 sec) mysql> select 1 from dual where exists (select id from t3 where id=2); Empty set (0.15 sec) mysql> select 1 from dual where exists (select max(id) from t3 where id=2); +---+ | 1 | +---+ | 1 |
很明显,id等于2的列不存在,但是第二条语句还是当做TRUE来处理了。
也确认了下两条语句的执行计划和改写后的SQL
第一个语句
mysql> EXPLAIN EXTENDED select 1 from dual where exists (select id from t3 where id=2); +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+ | 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Impossible WHERE | | 2 | SUBQUERY | t3 | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+------------------+ 2 rows in set, 2 warnings (0.00 sec) mysql> show warnings; +---------+------+-------------------------------------------------------------------+ | Level | Code | Message | +---------+------+-------------------------------------------------------------------+ | Warning | 1681 | 'EXTENDED' is deprecated and will be removed in a future release. | | Note | 1003 | /* select#1 */ select 1 AS `1` from DUAL where 0 | +---------+------+-------------------------------------------------------------------+
第二个语句
mysql> EXPLAIN EXTENDED select 1 from dual where exists (select max(id) from t3 where id=2); +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+ | 1 | PRIMARY | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | No tables used | | 2 | SUBQUERY | t3 | NULL | ALL | NULL | NULL | NULL | NULL | 1 | 100.00 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+----------------+ 2 rows in set, 2 warnings (0.00 sec) mysql> show warnings; +---------+------+-------------------------------------------------------------------+ | Level | Code | Message | +---------+------+-------------------------------------------------------------------+ | Warning | 1681 | 'EXTENDED' is deprecated and will be removed in a future release. | | Note | 1003 | /* select#1 */ select 1 AS `1` from DUAL where 1 | +---------+------+-------------------------------------------------------------------+ 2 rows in set (0.00 sec)
执行计划及改写后的SQL确实有所不同,看来,确实是MySQL的一个bug了。
于是,给官方提了个bug
http://bugs.mysql.com/bug.php"htmlcode">
mysql> select 1 from dual where exists (select abs(id) from t3 where id=2); Empty set (0.07 sec) mysql> select 1 from dual where exists (select floor(id) from t3 where id=2); Empty set (0.00 sec)
以上所述是小编给大家介绍的MySQL关于exists的一个bug ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
mysql,exists
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 群星《歌手2024 第4期》[FLAC/分轨][563.76MB]
- RiffKitten-ChaosParade(2024)[24-44,1]FLAC
- RuneKlakegg-Nattevandrer(2024)[24-96]\FLAC
- 赖冰霞.2006-金嗓典藏辑2IN1(南方金点系列)2CD【南方】【WAV+CUE】
- 群星.1988-国语金曲尽精英VOL.2【瑞成】【WAV+CUE】
- 张宇.1999-雨一直下【EMI百代】【WAV+CUE】
- 袁娅维.2021-月亮失眠了(DELUXE)【华纳】【FLAC分轨】
- AlanBroadbent-JazzFunk(2024)[24-44,1]
- 梅艳芳《绝代芳华》开盘母带[低速原抓WAV+CUE]
- ABC唱片-《外国精逊母带直刻神奇黑胶[APE+CUE].
- 群星《天赐的声音第五季 第6期》[320K/MP3][101.43MB]
- 群星《天赐的声音第五季 第6期》[FLAC/分轨][526.16MB]
- 群星《说唱梦工厂 第2期》[320K/MP3][96.37MB]
- 杨宗宪.1994-留你留袂着【有容唱片】【WAV+CUE】
- 李翊君.2005-勇敢的爱【创意唱片】【WAV+CUE】