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/