mysql pgsql json array specified condition traversal query through select specified condition to perform traversal query matching in json array, without writing additional functions
mysql pgsql json array specified condition traversal query through select specified condition to perform traversal query matching in json array, without writing additional functions
createtable students
(
name varchar,
gender varchar,
age int,
scores jsonb
);INSERTINTOpublic.students (name, gender, age, scores)VALUES('张三','男',34,'[{"语文": "78"},{"数学": "90"}]');INSERTINTOpublic.students (name, gender, age, scores)VALUES('李四','女',21,'[{"语文": "89"},{"数学": "67"}]');INSERTINTOpublic.students (name, gender, age, scores)VALUES('王二','女',27,null);select*from students ;
students
name
gender
age
scores
Zhang San
male
34
[{"Chinese": "78"}, {"Mathematics": "90"}]
John Doe
female
21
[{"Chinese": "89"}, {"Mathematics": "67"}]
Wang Er
female
27
NULL
pgsql
SQL
select*,(scores ->0)->>'语文' ::textas score
from students
where scores isnotnullANDexists(select1from jsonb_array_elements(students.scores) r
where r->>'语文'>'80');