带睡眠功能的 where 子句中的破折号 (-)

dash ( - ) in where clause with sleep function

在 MySQL 中,您可以使用 SLEEP function.

将结果暂停 x 秒

当你这样使用它时我明白了:

SELECT SLEEP(1000);

但我最近看到您可以用这种形式增强查询:

mysql> select * from docs where rev = 1-sleep(4);

它会等待 4 秒,然后 returns 结果与

相比
mysql> select * from docs where rev = 1;

为什么会这样?破折号是一种连接函数的形式还是类似的形式?我找不到此语法的任何解释。


我使用 docker

复制了这个查询
docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
docker exec -ti some-mysql mysql -uroot -pmy-secret-pw

和以下代码创建 table(使用来自 sql fiddle 的代码)并验证行为。

CREATE test;
USE test;
CREATE TABLE IF NOT EXISTS `docs` (
  `id` int(6) unsigned NOT NULL,
  `rev` int(3) unsigned NOT NULL,
  `content` varchar(200) NOT NULL,
  PRIMARY KEY (`id`,`rev`)
) DEFAULT CHARSET=utf8;
INSERT INTO `docs` (`id`, `rev`, `content`) VALUES
  ('1', '1', 'The earth is flat'),
  ('2', '1', 'One hundred angels can dance on the head of a pin'),
  ('1', '2', 'The earth is flat and rests on a bull\'s horn'),
  ('1', '3', 'The earth is like a ball.');
select * from docs where rev = 1;
select * from docs where rev = 1-sleep(4);

正如您在链接的手册中所读到的,SLEEP returns 0。因此在某种计算中使用它,例如 where rev = 1-sleep(4); 会触发 SLEEP(如:等待四秒钟)。之后,查询以 where rev = 1-0 恢复,使其等于 where rev = 1