Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySql SELECT found row when WHERE doesn't match

I have a pretty simple table called roles:

enter image description here

When I ran SELECT * FROM roles WHERE role_id = "1ojosd041l"(the WHERE clause clearly didn’t match with any row in the table). It somehow returned this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

Does anyone know why this is happening? My guess is that my role_id column is of type int, but I passed a string into the WHERE clause, so the str to int conversion produced something weird (like a 0 or 1).

I’m just looking for an official explanation for this. If anyone knows why this happens please let me know! Thank you!

>Solution :

There is somee MySQL magic happening here. MySQL sees that role_id is numeric. So it converts your string ‘1ojosd041l’ to a number. This string isn’t a number obviously, and MySQL should throw an error. But MySQL just takes as many digits as it finds from the left side of your string instead, so it gets number 1. (If your string started with a non-digit, then MySQL would even return a zero.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading