开发者

Why do the single quotes in this SQL query affect the calculations?

开发者 https://www.devze.com 2023-02-07 14:36 出处:网络
SELECT COUNT(*) FROM planets WHERE ROUND(SQRT(POWER((\'71\' - coords_x), 2) + POWER((\'97\' - coords_y), 2))) <= 17
SELECT COUNT(*) FROM planets
WHERE ROUND(SQRT(POWER(('71' - coords_x), 2) +
                 POWER(('97' - coords_y), 2))) <= 17

==> 51

SELECT COUNT(*) FROM planets
WHERE ROUND(SQRT(POWER((71 - coords_x), 2) +
                 POWER((97 - coords_y), 2))) <= 17

==> 22

coords_x and coords_y are both TINYINT fields containing values in the range [1, 100]. Usually MySQL doesn't care if numbers are quoted or no开发者_运维问答t.. but apparently it does in this case. The question is: Why?


I am a bit rusty on the inerds of MySql but <= on string goes to lexicographical sorting instead of numeric ie, '150' < '17'.


The implicit conversion from string to floating point number is probably causing in inaccurate results. See: Type Conversion in Expression Evaluation

0

精彩评论

暂无评论...
验证码 换一张
取 消