1

This statement

select 'foo' in ('foo', 'bar');

returns 1 as expected.

This

select json_extract('["foo"]', '$[0]') = 'foo';

also returns 1 as expected.

However, this

 select json_extract('["foo"]', '$[0]') in ('foo', 'bar');

unuexpectedly returns 0. Even more strangely, if written with additional quotes

select json_extract('["foo"]', '$[0]') in ('"foo"', '"bar"');

it returns 1 instead. But this does not work with simple equality, as

select json_extract('["foo"]', '$[0]') = '"foo"';

returns 0.

So the question is: why "IN" and "=" behave differently only when fed with JSON results?

1
  • select json_extract('["foo"]', '$[0]') in ('foo', 'bar'); returns 1 on my system.... (MySQL8.0.25/Windows) Commented Jun 29, 2021 at 8:56

1 Answer 1

3

Version-dependent. MySQL 5.7 needs in additional JSON_UNQUOTE().

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=bcfea5bebdc270c77e1edbc2c384909f

https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=bcfea5bebdc270c77e1edbc2c384909f

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.