mysql 8.0 执行 sql 语句 中 where 条件中 in 的子查询是错误的,但是完整的 sql 语句执行是成功

181 天前
 liaowb3

大佬们,我遇到一个很奇怪的 sql 问题,首先我先展示两个建表语句

CREATE TABLE `monitor_message` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `monitor_id` int(11) NOT NULL DEFAULT '0',
  `monitor_type` varchar(255) NOT NULL DEFAULT '',
  `run_time` datetime NOT NULL,
  `type` varchar(255) NOT NULL 
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=5238 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;

CREATE TABLE `monitor_config` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `project_id` varchar(128) NOT NULL DEFAULT '',
  `monitor_type` varchar(255) NOT NULL DEFAULT '',
  `name` varchar(512) NOT NULL DEFAULT '',
  `state` varchar(64) NOT NULL DEFAULT '启用',
  `json_config` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=257 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;

然后下面那个语句能执行成功

		select
			id,
			monitor_id,
			monitor_type
		from
			monitor_message
		where
			run_time >= '2023-11-07'
			AND run_time < '2023-11-08'
			AND monitor_id IN (
				SELECT
					monitor_id
				FROM
					monitor_config
				WHERE
					project_id = '123'
			)
		order by run_time desc

但是其中的子查询是有问题的,单独执行是失败的,为啥会执行成功呢

1402 次点击
所在节点    MySQL
12 条回复
thevita
181 天前
liaowb3
181 天前
@thevita 我看到你发的链接是关于表达式求值中的类型转换,但是我不太能关联到我这边这个问题,所以能稍微具体说一下是什么个问题吗
thevita
181 天前
@liaowb3 ...看错了,ignore me
adoal
181 天前
子查询单独执行时又不知道 monitor_id 是哪里来的
wps353
181 天前
fujizx
181 天前
monitor_config 表里没有 monitor_id 啊
foursevenlove
181 天前
楼上正解
dsioahui2
181 天前
另外这个语句改成 join 性能会有成倍的提高,比如
```sql
select
id,
monitor_id,
monitor_type
from
monitor_message t1 join monitor_config t2
on t1.monitor_id = t2.id (不知道你是要关联哪个字段,姑且按照 id 了)
where
t1.run_time >= '2023-11-07'
AND t1.run_time < '2023-11-08'
AND t2.project_id = '123'
order by run_time desc
```
wu00
181 天前
题目和内容都说的很清楚啊...
我也试了下还真是,好像子查询异常被吃了一样最终生成
SELECT * FROM table1 WHERE mid IN ( SELECT NULL )
whorusq
181 天前
IN() 操作符允许使用 NULL 值
Rache1
181 天前
DataGrip 里面执行的时候提示了这里是外部的列


explain analyze 的结果如下

-> Sort: monitor_message.run_time DESC (actual time=0.048..0.048 rows=0 loops=1)
-> Stream results (cost=0.70 rows=1) (actual time=0.035..0.035 rows=0 loops=1)
-> Hash semijoin (no condition) (cost=0.70 rows=1) (actual time=0.033..0.033 rows=0 loops=1)
-> Filter: ((monitor_message.run_time >= TIMESTAMP'2023-11-07 00:00:00') and (monitor_message.run_time < TIMESTAMP'2023-11-08 00:00:00')) (cost=0.35 rows=1) (never executed)
-> Table scan on monitor_message (cost=0.35 rows=1) (never executed)
-> Hash
-> Limit: 1 row(s) (cost=0.35 rows=1) (actual time=0.027..0.027 rows=0 loops=1)
-> Filter: (monitor_config.project_id = '123') (cost=0.35 rows=1) (actual time=0.026..0.026 rows=0 loops=1)
-> Table scan on monitor_config (cost=0.35 rows=1) (actual time=0.020..0.024 rows=1 loops=1)
Rache1
181 天前
@Rache1 #11 缩进居然没了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/990249

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX