我是靠谱客的博主 光亮裙子,这篇文章主要介绍mysql pgsql json数组指定条件遍历查询 通过select指定条件在json数组中做遍历查询匹配,不另外写函数,现在分享给大家,希望可以做个参考。
mysql pgsql json数组指定条件遍历查询 通过select指定条件在json数组中做遍历查询匹配,不另外写函数
- 数据库服务器环境
- 原表结构和数据
- students
- pgsql
- SQL
- 结果
- mysql(TODO。。。
- SQL
- 结果
- 相关文章导读
数据库服务器环境
| 数据库 | 版本 |
|---|---|
| mysql | 5.7.34 |
| PostgreSQL | 10.3 |
原表结构和数据
create table students
(
name varchar,
gender varchar,
age int,
scores jsonb
);
INSERT INTO public.students (name, gender, age, scores) VALUES ('张三', '男', 34, '[{"语文": "78"},{"数学": "90"}]');
INSERT INTO public.students (name, gender, age, scores) VALUES ('李四', '女', 21, '[{"语文": "89"},{"数学": "67"}]');
INSERT INTO public.students (name, gender, age, scores) VALUES ('王二', '女', 27, null);
select *
from students ;
students
| name | gender | age | scores |
|---|---|---|---|
| 张三 | 男 | 34 | [{“语文”: “78”}, {“数学”: “90”}] |
| 李四 | 女 | 21 | [{“语文”: “89”}, {“数学”: “67”}] |
| 王二 | 女 | 27 | NULL |
pgsql
SQL
select *, (scores -> 0 )->>'语文' ::text as score
from students
where scores is not null
AND exists(
select 1
from jsonb_array_elements(students.scores) r
where r->>'语文' > '80'
);
结果
| name | gender | age | scores | score |
|---|---|---|---|---|
| 李四 | 女 | 21 | [{“语文”: “89”}, {“数学”: “67”}] | 89 |
mysql(TODO。。。
SQL
结果
相关文章导读
mysql 在select查询语句中使用临时变量累计求和 ;
相同列值的记录中再根据条件取其中最大或最新一条;
mysql经纬度求距离并排序
mysql pgsql 实现多行记录合并成一行 分组合并 用指定字符做分割
mysql pgsql 多行记录转JSON数组字段 行转json列
最后
以上就是光亮裙子最近收集整理的关于mysql pgsql json数组指定条件遍历查询 通过select指定条件在json数组中做遍历查询匹配,不另外写函数的全部内容,更多相关mysql内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复