如何从mysql json字段中按值顺序提取字段
| id | json_field |
| 1 | {"a": 1, "b" : 3, "c": 2, "d": -1} |
我想获取具有最小值的两个字段(我不确定我是否表达清楚),在本例中是a
和<代码>d。
塞纳里奥: 给定一个字段,我想检查该字段是否在 json_field 中具有最小值的两个字段之一,并获取与此条件匹配的所有结果。
我想通过以下方式实现它: 按值对 json 进行排序,将其字段转换为数组,然后获取最后/前两个。 在我四处搜索之后,我找不到按值对 json 字段进行排序的方法。我被困住了。
顺便说一句,我使用的是 MySQL5.7。
| id | json_field |
| 1 | {"a": 1, "b" : 3, "c": 2, "d": -1} |
I want to get the two fields which have the smallest values(I not sure I am expressing clearly), in this case is a
and d
.
Senario:
given a field, I wanna check if this field is among the two fields which have smallest values in json_field
, and fetch all results match this condition.
I wanna implement it by:
Sort json by value, convert it's fields to an array, and then get the last/first two one.
After I have searched around, I cannot find a way to sort json field by value. I am stuck.
BTW, I am using MySQL5.7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论