现在有张表,id、yarn_attr等其他字段,其中yarn_attr存储的格式是json。现在需要根据条件,查询json对象里面的值,该怎么操作?
yarn_attr的格式如下:
{
\"dye_id\": null,
\"dye_name\": \"\",
\"light_id\": 39,
\"cotton_id\": null,
\"dye_index\": -1,
\"light_name\": \"全啞光\",
\"cotton_name\": \"\",
\"light_index\": 1,
\"cotton_index\": -1
}
查询代码片段如下:
$where = array_filter($where); //过滤掉空值
//组成SQL语句
$yarn_attr = $data[\'yarn_attr\'];
$sql = \"\";
foreach ($yarn_attr as $key => $value) {
if (!empty($value)) {
//非数字需要加引号
if (!is_numeric($value)) {
$sql .= \'yarn_attr->\"$.\' . $key . \'\"=\"\' . $value . \'\" and \';
} else {
$sql .= \'yarn_attr->\"$.\' . $key . \'\"=\' . $value . \' and \';
}
}
}
$sql = rtrim($sql, \" and \");
$count = YarnCheck::query()->where($where)->whereRaw($sql)->whereNull(\'deleted_at\')->count();
继续阅读与本文标签相同的文章
上一篇 :
CPU到底是什么?
下一篇 :
CNN超参数优化和可视化技巧详解
-
程序员的天敌,作为一名程序员新人怎样在复杂代码中找bug
2026-05-18栏目: 教程
-
东京车展丰田玩把大的 人工智能自动驾驶都有 新RAV4也将混动亮相
2026-05-18栏目: 教程
-
“北斗+”为长沙公共安全“站岗”
2026-05-18栏目: 教程
-
滴滴与清华大学成立未来出行联合研究中心
2026-05-18栏目: 教程
-
三星S10被曝屏下指纹存在安全漏洞:任何人都还可以解锁
2026-05-18栏目: 教程
