php - Laravel sql搜索json类型列

标签 php mysql json laravel-5

mysql有表'subscribe'表如下:

column   type
id        int
condition  json
type_id    id

示例如下:

"id": "1",
"condition": "{\"id\":\"2\",\"class\":\"master\",\"zone\":\"west\",\"price\":\"511\"}",
"type_id": "1"

我想选择在列条件中匹配的列,如“zone”=“west” laravel 5.2 已经支持命令

 $subscribe = DB::table('subscribe')->where('condition->class', 'master')->get();

错误

 Column not found: 1054 Unknown column 'condition->class' in 'where clause'(
SQL: select * from `subscribe` where `condition->class` = master)

我需要的是获得符合条件的术语 where conditon->class = master 。我需要选择符合模型需要的数据。 我不知道出了什么问题。任何帮助将不胜感激。

最佳答案

我认为正确的语法是:

 $subscribe = DB::table('subscribe')->where('condition->"$.class"', 'master')->get();

请参阅本段下方的示例,关于 https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html

  • column->path

In MySQL 5.7.9 and later, the -> operator serves as an alias for the JSON_EXTRACT() function when used with two arguments, a column identifier on the left and a JSON path on the right that is evaluated against the JSON document (the column value). You can use such expressions in place of column identifiers wherever they occur in SQL statements.

关于php - Laravel sql搜索json类型列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36957717/

相关文章:

php - 如何通过 PHP 在 MySQL 中插入 HTML 选择值作为文本

php - file_get_contents() 返回 "failed to open stream: File name too long",但 url 从浏览器中打开正常

php - 使用户处于事件/非事件状态 PHP MySQL

javascript - 在 JavaScript 中使用 PHP 的值

python - 如果只能获取完整的用户列表,则从 REST GET 读取单个用户条目

sql - PostgreSQL 中 JSON 数据类型的正确用例是什么?

php - 如何使用 PHP cURL 登录 mediawiki?

php事件系统实现

mysql - 请帮我分析一下Mysql慢查询

MySQL控制台错误未知选项 '--root'