-1

I have a table users as,

id    name     status
---------------------
1     user1      1
2     user2      1
3     user3      1

When I run this query it will select the record with id 2. Why this happened? I need to select record with id 2 when only the id is 2. I did the query like,

SELECT *
FROM (
`users`
)
WHERE `id` = '2a290cf764371';

How can I fix this issue?

2
  • There is an implicit type conversion. This means '2a290cf764371' is converted to 2 and then compared to id. To solve the problem you have to add some type checking validation on the client end. Commented Dec 7, 2017 at 10:34
  • Possible duplicate of MySQL automatic string to integer casting in where clause? Commented Dec 7, 2017 at 11:06

1 Answer 1

2

That happens because you are checking a string against an integer, so mysql autocast the string to integer and the integer of 2a290cf764371 is 2, don't do this kind of comparation, i suggest you to cast every ID to int before execute the query

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.