I have a JSON field in the stadiums table (mysql) named location, can i compare it with another json like below?...
select *from stadiums where location = '{"lat":40, "lng":3}';
*The query doesnt return me any error, but doesnt return me any row when there is in fact a coincidence
You can use CAST() function:
SELECT * FROM stadiums WHERE location = CAST('{"lat":40, "lng":3}' AS JSON);
MySQL has JSON_SEARCH JSON_CONTAINS JSON_EXTRACT functions that you can use to search JSON fields.
Try something like:
select * from stadiums where
JSON_EXTRACT(location, '$.lat') = 40 and
JSON_EXTRACT(location, '$.lng') = 3;
Related
I have a JSON column and the data stored looks like:
{"results":{"made":true,"cooked":true,"eaten":true}}
{"results":{"made":true,"cooked":true,"eaten":false}}
{"results":{"made":true,"eaten":true,"a":false,"b":true,"c":false}, "more": {"ignore":true}}
I need to find all rows where 1+ values in $.results is false.
I tried using JSON_CONTAINS() but didn't find a way to get it to compare to a boolean JSON value, or to look at all values in $.results.
This needs to work with MySQL 5.7 but if it's not possible I will accept a MySQL 8+ answer.
I don't know the way for to search for a JSON true/false/null value using JSON functions - in practice these values are treated as string-type values during the search with JSON_CONTAINS, JSON_SEARCH, etc.
Use regular expression for the checking. Something like
SELECT id,
JSON_PRETTY(jsondata)
FROM test
WHERE jsondata REGEXP '"results": {[^}]+: false.*}';
DEMO
You could simply search the JSON_EXTRACT using the LIKE condition this way.
SELECT * FROM table1 WHERE JSON_EXTRACT(json_dict, '$.results') LIKE '%: false%';
Check this DB FIDDLE
An alternative to the pattern matching in other answers, is to extract all values from $.results and check each entry with a helper table with running numbers
SELECT DISTINCT v.id, v.json_value
FROM (
SELECT id, json_value, JSON_EXTRACT(json_value, '$.results.*') value_array
FROM json_table
) v
JOIN seq ON seq.n < JSON_LENGTH(v.value_array)
WHERE JSON_EXTRACT(v.value_array, CONCAT('$[', seq.n, ']')) = false
Here is the demo
I have table name players where I store json array into stats column, which is like following
{"sbuilders":{"perfect_builds":2,"rounds_wins":3,"games_played":10},
"ffa":{"uhc":{"deaths":12,"kills":8},
"op":{"kills":5,"deaths":1},
"classic":{"deaths":70,"kills":115}}}
But how can I get top 10 sbuilders game_played from this table?
I haven't worked with JSON in mysql much, so this was interesting.
You can use JSON_EXTRACT to extract a json attribute from a json object; you can then use this result as you desire.
select JSON_EXTRACT(i.j, '$.sbuilders.games_played') as gamesplayed from i;
https://www.db-fiddle.com/f/mNiqfif4si7BMZG4vA5cvJ/0
It seems that you need in
SELECT *
FROM players
ORDER BY JSON_EXTRACT(stats, '$.sbuilders.games_played') DESC
LIMIT 10
In a word, I want to
select * from test.population where Number in (1,2,3),
but in the place of (1,2,3) I want to have a function that returns json array. So that I want to have this to be working like this.
select * from test.population where Number in ('[1,2,3]')
How to put json-array into where it clause?
You can use the MEMBER OF operator:
Number member of ('[1,2,3]')
You can use JSON_SEARCH(). It is available since MySQL 5.7, whereas MEMBER OF() came with MySQL 8.0:
select * from test.population where json_search('[1,2,3]', 'one', number) is not null
I am using postgres v9.3
I have a table called temp which have a column all_data. The value looks something like below :-
{"Accountid" : "1364", "Personalid" : "4629-87c3-04e6a7a60208", "quote_number" : "QWQA62364384"}
Now, I want to query the all_data column by accountid=1364.
Could you please tell what would be the query?
Use the ->> operator
select *
from temp
where all_data ->> 'Accountid' = '1364';
Online example for 9.3: http://sqlfiddle.com/#!15/55981/1
However the above will not work if the JSON contains an array instead of a JSON object. E.g. a value like '[1,2]'::json will cause that query to fail.
With 9.4 and above you could check that using json_typeof() but with your soon to be unsupported version the only workaround I can think of is to convert the column to text and exclude those that start with a [
with valid_rows as (
select *
from temp
where all_data::text not like '[%'
)
select *
from valid_rows
where all_data ->> 'Accountid' = '1364';
Online example for 9.3: http://sqlfiddle.com/#!15/d01f43/3
I have a column which stores values in array form like below
table_name : records
column_name : data sample
row 1 : ['John','Adam', 'Mike']
I am trying to apply query something like below
SELECT * FROM records WHERE data IN('Adam');
which is giving 0 results found.
The IN clause with one parameter is essentially the same as WHERE data = 'Adam', wich search for the exact value.
Since your row contains a different string from Adam, no results are returned.
The query for your situation should be something like this:
SELECT * FROM records WHERE data LIKE '%Adam%';
If you using JSONArray , you can try query using JSON_CONTAINS() function.
you can query like below:-
SELECT * FROM records WHERE JSON_CONTAINS(data, '[Adam]');
where data is column which contains the array.