MySQL how to search a JSON field for all rows that intersect with JSON values

Darren Gates

Assume that I have a MySQL table with a JSON field, with the structure & values shown below:

CREATE TABLE `projects` (
  `project_ids` json DEFAULT NULL
) ENGINE=InnoDB;

INSERT INTO `projects` (`project_ids`)
VALUES
    ('[1, 2, 3]'),
    ('[1, 2]'),
    ('[2]'),
    ('[1]'),
    ('[2, 3]');

I would like to get all rows that have ANY of the JSON values that match in a SELECT query.

For example:

SELECT * FROM projects WHERE JSON_CONTAINS(project_ids, '[1,2]');

This yields 2 rows:

[1, 2, 3]
[1, 2]

However, I would like it to yield all of the rows, because every row has at least a "1" or a "2" in the JSON. In other words, I'm looking for a query that will return rows where the JSON intersection is non-empty.

Thus, I WANT it to return:

[1, 2, 3]
[1, 2]
[2]
[1]
[2, 3]

Please ignore the larger question of whether it's even a good idea to use JSON fields over foreign keys, etc. Assume that I have to use a JSON field.

Gordon Linoff

OR is the simplest method:

SELECT *
FROM projects
WHERE JSON_CONTAINS(project_ids, '[1]') OR
      JSON_CONTAINS(project_ids, '[2]') ;

But if you want to pass in the JSON array, use JSON_OVERLAPS():

SELECT *
FROM projects
WHERE JSON_OVERLAPS(project_ids, '[1,2]') ;

Here is a db<>fiddle.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Mysql extract json data and search multiple values

分類Dev

How to search into nested JSON by values with Python

分類Dev

Extract a key from list of key,values pairs in json field mysql

分類Dev

Search for data values in JSON object

分類Dev

python how to search a string, count values and group by in json

分類Dev

Retrieve a json field from mysql field with PDO

分類Dev

How to serializer django-mysql's JSON field

分類Dev

How to parse json to get all values of a specific key within an array?

分類Dev

How to get JSON response array all index values?

分類Dev

Is there a way how to compare array from JSON field against JSON_ARRAY in MySQL?

分類Dev

Getting specific field values from Json Python

分類Dev

Active Record Update All JSON Field

分類Dev

MySql get rows into single JSON object

分類Dev

How To get All Rows in Mysql Database (Java)

分類Dev

How to avoid splitting of field values in faceted search in solr

分類Dev

MySQL show all but one properties in JSON

分類Dev

JavaScript: how do I remove all the white spaces from a JSON string except the ones in the values?

分類Dev

In Angular 8, How to get form all values into JSON on click submit button?

分類Dev

How to return rows of data from a table with json?

分類Dev

How To Pull Multiple JSON records with one search

分類Dev

How to append search keyword to twitter json data?

分類Dev

How to use the ActiveRecord json field type

分類Dev

How to add a field to a JSON object with the jq command?

分類Dev

How to drop all rows in pandas dataframe with negative values?

分類Dev

remove all values from JSON matching specific key

分類Dev

How to remove empty string json values in complex json object?

分類Dev

how to access nested Json key values in Golang

分類Dev

How to display JSON values from API

分類Dev

How to decode custom JSON values using JSONDecoder

Related 関連記事

  1. 1

    Mysql extract json data and search multiple values

  2. 2

    How to search into nested JSON by values with Python

  3. 3

    Extract a key from list of key,values pairs in json field mysql

  4. 4

    Search for data values in JSON object

  5. 5

    python how to search a string, count values and group by in json

  6. 6

    Retrieve a json field from mysql field with PDO

  7. 7

    How to serializer django-mysql's JSON field

  8. 8

    How to parse json to get all values of a specific key within an array?

  9. 9

    How to get JSON response array all index values?

  10. 10

    Is there a way how to compare array from JSON field against JSON_ARRAY in MySQL?

  11. 11

    Getting specific field values from Json Python

  12. 12

    Active Record Update All JSON Field

  13. 13

    MySql get rows into single JSON object

  14. 14

    How To get All Rows in Mysql Database (Java)

  15. 15

    How to avoid splitting of field values in faceted search in solr

  16. 16

    MySQL show all but one properties in JSON

  17. 17

    JavaScript: how do I remove all the white spaces from a JSON string except the ones in the values?

  18. 18

    In Angular 8, How to get form all values into JSON on click submit button?

  19. 19

    How to return rows of data from a table with json?

  20. 20

    How To Pull Multiple JSON records with one search

  21. 21

    How to append search keyword to twitter json data?

  22. 22

    How to use the ActiveRecord json field type

  23. 23

    How to add a field to a JSON object with the jq command?

  24. 24

    How to drop all rows in pandas dataframe with negative values?

  25. 25

    remove all values from JSON matching specific key

  26. 26

    How to remove empty string json values in complex json object?

  27. 27

    how to access nested Json key values in Golang

  28. 28

    How to display JSON values from API

  29. 29

    How to decode custom JSON values using JSONDecoder

ホットタグ

アーカイブ