简体   繁体   English

从MySql数据库的JSON列获取数据

[英]Get data from JSON column in MySql database

In my table there is a column with JSON encoded data and one record looks something like this 在我的表格中,有一列包含JSON编码的数据,一条记录如下所示

{"late_fee":10,
"due_date":"2019-10-01 00:00:00",
"property_type":"house"
}

When getting data from database I need records where key late_fee does not exist in JSON data column. 当从数据库中获取数据时,我需要在JSON数据列中不存在键late_fee记录。 How can I put it in where condition? 如何将其放在where

This is the line of code on which I have to add mentioned condition: 这是我必须在上面添加条件的代码行:

$payments = Rent::where('property_id', $property['id']);`

EDIT 编辑

By comments you posted, can you confirm me that this is the right way: 通过您发表的评论,您能否确认我这是正确的方法:

$payments = Rent::where('property_id', $property['id'])->whereJsonContains('notification_settings->late_fee', null);

PS notification_settings is the name of column where JSON data is stored. PS notification_settings是存储JSON数据的列的名称。

Just read this short article: https://laravel.com/docs/5.7/queries#json-where-clauses 只需阅读以下简短文章: https : //laravel.com/docs/5.7/queries#json-where-clauses

That should work : 那应该工作:

$payments = Rent::where('property_id', $property['id'])->whereJsonContains('notification_settings->late_fee', null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM