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

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]. Normally, MySQL doesn't care if the numbers are quoted or not .. but apparently this happens in this case. Question: Why?

+4
source share
2 answers

Implicit conversion from a string to a floating-point number probably leads to inaccurate results. See: Type Conversion in Expression Evaluation

+2
source

I'm a little rusty on inerds from MySql, but <= the line is lexicographically sorted instead of numerical, i.e. '150' < '17' .

+4
source

Source: https://habr.com/ru/post/1337554/


All Articles