0

The field cookingMethod is varchar. When I pass the number parameter: in(3,4) why do I get this result?

SELECT distinct(cookingMethod) FROM recipe where cookingMethod in(3, 4);

Result: [3][3,5][3,4][4][4,5]

enter image description here

and if I use ('3','4'), the result is only '3','4'?

SELECT distinct(cookingMethod) FROM recipe where cookingMethod in('3', '4')

Result: [3][4]

2
  • Now what is your problem anyway? Commented Nov 3, 2017 at 3:48
  • not yet, SELECT distinct(cookingMethod) FROM cms.recipe where cookingMethod = 3 result : [3,1,5] [3,5] [3] Commented Nov 3, 2017 at 6:33

1 Answer 1

1

Your second query is simple, and returns the expected result, because you are comparing strings with strings.

In your first query, however, you are using numbers for comparison with strings. MySQL implicitly tries to convert the value of the field into a number to perform the comparison (where other databases might just have thrown an error). However, since there field has both numbers and commas, MySQL treats it as a BLOB with a comma-separated list of numbers, for purposes of this query. The IN operator will search through a comma-separated list, and therefore it matches every field that has either a 3 or a 4 within it's comma-separated list of numbers.

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.