Search on field with json data - json

i have a json array, f.e. [169,172] and i need a query to find if this array contains a number, f.e.:
SELECT * FROM smthg WHERE 169 IN '[169,172]'::json;
How can i do this?

select '[169,172]'::jsonb #> '169';
this is for postgres 9.4b2 using the jsonb type. For lower version numbers you may have to resort to a text search or something like this:
select true where '169' in (select json_array_elements_text('[169,172]'::json));

Related

find exact number in text field in sql

I am trying to query exact number in array inside text field in sql
so for example
SELECT tbl_campaigns.s_id,a_list_fk_i_id_tbl_affiliates
FROM tbl_campaigns
WHERE a_list_fk_i_id_tbl_affiliates LIKE '%1%'
can return
[3,8,7,11]
which is not good because i would like to get the arrays that contain 1
You could use JSON functions:
SELECT s_id,a_list_fk_i_id_tbl_affiliates
FROM tbl_campaigns
WHERE JSON_CONTAINS(a_list_fk_i_id_tbl_affiliates, 1)

SQL regex to check if within JSON array there exists at least one item

I have a table in MySQL 5.6 with a text type field "custom_info" which exists JSON as string. Such a JSON's exist field "clients" with array.
In order to select records where "clients" is empty I use query:
select custom_info from users where custom_info like '%"clients":[]%'
How could I retrieve records where "clients" array exist at least one element?
MySQL natively supports JSON. Instead of storing as a string literal, I recommend storing it as a JSON data type. In doing so, it will open up several native functions like JSON_CONTAINS, JSON_EXTRACT, JSON_ARRAY, and JSON_OBJECT.
You could then use JSON_CONTAINS or JSON_EXTRACT to evaluate your results; something like:
SELECT *
FROM custom_info
WHERE JSON_CONTAINS(clients, 'some_value_youd_expect_to_be_here')
Or:
SELECT *
FROM custom_info
WHERE JSON_EXTRACT('clients', '$[0]') is not null
Since you want to only return entries having no ] after "clients":[ you may use
where custom_info REGEXP '"clients":\\[[^]]'
The \\[ is actually a \[ pattern matching a literal [ char, and [^]] is a negated bracket expression matching any char butr a ].
In the case you want to keep your field as text type you could use _% which will match at least one character
select custom_info from users where custom_info like '%"clients":[_%]%'

How to select from a json encoded field that contains nested data?

The fields looks like this:
[{"Tester":"Bonnie","Credentials":"MS","Date":"2013-02-19"},
{"Tester":"Karen","Credentials":"Teacher","Date":"2016-01-20"}]
I need to select based on the year of the 'Date' value: < > or =
I have found the json_extract function and it's shortcuts such that this will get the data (from the MySQL docs) "autowrapped as an array" and it works:
json_field->"$[*].Date" returns ["2013-02-19", "2016-01-20"]
Great, so I has the dates from the json data, but now I need to format the WHERE. How do I select the record if either of the years is 2016 for instance? I don't see any json function that do that.
I would highly recommend you to drop this JSON column and build another table, then you can search in your table using JOIN. JSON in column is a bad idea.
Just to answer your question:
I think this should work -
WHERE YEAR(STR_TO_DATE(json_field->"$.Date", '%Y-%m-%d')) = '2016'

MySql, How to select data by comparing 2 Jsons

I have a json data like this
{"0":"6","1":"5","2":"10"}
And on the DB I have table which contains json datas like these
{"0":"6","1":"4"}
{"0":"5","1":"2","2":"7"}
{"0":"3","1":"10","2":"4"}
{"0":"6","1":"5","2":"10","3":"8"}
So, I would like know is it possible or does it make sense to select data by comparing the json datas?
I would like to get any json that may contain any key:value in my input json.
So, from my example they will be these
{"0":"6","1":"4"}
{"0":"6","1":"5","2":"10","3":"8"}
You can use JSON search functions. For example -
SELECT json_field FROM table1
WHERE
JSON_CONTAINS(json_field, '{"0":"6"}')
AND JSON_CONTAINS(json_field, '{"1":"5"}')
AND JSON_CONTAINS(json_field, '{"2":"10"}');

Postgres select where jsonb array contains one or more elements in another array

I've got a postgres column 'data' which is jsonb in this format:
{
'tags': ['friend','enemy','frenemy']
... // other data
}
Say I want to select all rows that are tagged either 'enemy' or 'frenemy', how can i do that?
I know I can select on a single tag with
SELECT * FROM people WHERE people.data->'tags' ? 'enemy'
I should be using #> ANY, but can't quite figure out the syntax for the to make it work.
I found the answer tucked away in this SO question.
SELECT * FROM people WHERE people.data->'tags' ?| ARRAY['enemy','frenemy']