0
SELECT * FROM `size_details` WHERE size_details.standard = 20

Results

enter image description here

SELECT * FROM `size_details` WHERE size_details.standard = '20'

Results

enter image description here

3
  • 1
    the results are correct :-) . in your first sample you compare the string with a number (20). MySQL tried to convert the string to an integer and stops at the first bad char (x). so the compare is true and in the resultset. in the second you compare string with string Commented Nov 2, 2021 at 21:57
  • See this demo here Commented Nov 3, 2021 at 0:27
  • @BerndBuffen MySQL tried to convert the string to an integer and stops at the first bad char (x). No. Both operands are converted to floating-point before comparing. Commented Nov 3, 2021 at 4:38

1 Answer 1

4

In your first example the constant you're comparing against is expressed as an integer so MySQL attempts to cast the value from the table to an integer for comparison. It will proceed character by character until it encounters a non-numeric character.

In your example 20x20 will cast to 20 because the conversion stops at x. This passes the comparison.

In your second example both parts of the comparison are strings and '20x20' is not equal to '20' so the comparison fails.

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

2 Comments

so MySQL attempts to cast the value from the table to an integer for comparison. No. Both operands are converted to floating-point (real) before comparing. See Type Conversion in Expression Evaluation - the most last rule is applied.
@akina thanks for the link. I spent about 20 minutes hunting for that. Clearly my Google-foo was lacking today.

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.