关于 MySql 中存在评论的结果问题

Question about the result with the presence of a comment in MySql

对 MySql

中存在评论的结果表示怀疑

最近我遇到了一个代码问题,我得到了一个奇怪的结果。几分钟后,我偶然发现了结果。我做了这个简单的测试来检查我的查询是否有问题:

select
NOW()as date_1,
'4: 33.32%' as string_1, 
--- comment,
NOW() as date_2, 
--- comment
'4: 33.32%' as string_2 

我得到了这个结果:

偶然,我对两条评论做了这个小改动:

select
NOW()as date_1,
'4: 33.32%' as string_1, 
--- comment,
NOW() as date_2, 
--- comment
'4: 33.32%' as string_2, 
-- - comment,
NOW() as date_3, 
-- - comment
'4: 33.32%' as string_3

有了这个,我有正确的答案:

但是,我对第二个日期和字符串之前出现的评论有疑问。为什么第三个连续的连字符会影响结果,它强制执行的计算是什么。

版本:10.3.8-MariaDB

SQL 条评论以 --<space> 开头。当你写 ---<space> 时,第一个 - 不是注释的一部分,它是注释 之前的减号 。所以就像你写的

select
NOW()as date_1,
'4: 33.32%' as string_1, 
- -- comment,
NOW() as date_2, 
- -- comment
'4: 33.32%' as string_2, 
-- - comment,
NOW() as date_3, 
-- - comment
'4: 33.32%' as string_3

当你删除评论时,这相当于

select
NOW()as date_1,
'4: 33.32%' as string_1, 
- NOW() as date_2, 
- '4: 33.32%' as string_2, 
NOW() as date_3, 
'4: 33.32%' as string_3

运算符-首先将其操作数转换为数字,因此NOW()中的日期被转换为数字20190621200233,字符串'4: 33.32%'被转换到数字 4.000。然后 - 运算符 returns 这些数字的负数,因此您会在结果中看到 -20190621200233-4.000