我正在使用 codeigniter,并且我在列 () 上有一个数字列表(例如
) 的表名 ['1', '2', '12', '42']
tags_jsonsubmit
,我想检查数字(例如“2”)是否在 tags_json
中,以便显示数据该行的内容。
我试过的代码是:
$this->db->select('*', FALSE);
$this->db->where("JSON_CONTAINS(tags_json,'[2]'");
$Query = $this->db->get('submit');
echo $Query->num_rows();
结果是:
Error Number: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 3
SELECT * FROM
submit
WHERE JSON_CONTAINS(tags_json,'[2]' IS NULL Filename: controllers/Home.php Line Number: 281
它在 CI 中的表现如何?
最佳答案
我已经找到了解决您问题的方法。使用此代码...
$this->db->select('*');
$this->db->where("JSON_CONTAINS(tags_json, JSON_ARRAY('2'))");
$Query = $this->db->get('submit');
echo $Query->num_rows();
如果没有找到,它将输出1和0。
如果它会给你这个错误
Invalid JSON text in argument 1 to function json_contains: "Invalid value." at position 1.
SELECT * FROM
submit
WHERE JSON_CONTAINS(tags_json, JSON_ARRAY('2'))
这是因为你的json数组无效所以确保你有这个格式
["1", "2", "12", "42"]
或者你可以像这样 [1, 2, 12, 42]
然后调用 JSON_ARRAY(2)
希望对您有所帮助。
关于php - 查询 ci 的 mySQL 表中有 JSON 列的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57957752/