我有一个数据库,其中包含 json 字符串的归档 myfield
:["2","4","1","6"]
或 [“1”]
。我想为此字段创建一个过滤器,并考虑使用以下方式查询它:
SELECT id FROM table_name WHERE myfield = '5'
-> 此返回无法回显 id,因为 ["2","4","1 ","6"]
没有值 5。
SELECT id FROM table_name WHERE myfield = '1'
-> return 可以回显 id-> 因为 -> 在归档 myfield
的 json 字符串中> 值为 1。
如何解决?我该怎么办?
我尝试过,但它对我不起作用:
从 table_name WHERE myfield = '1' 中选择 id
最佳答案
例如,要检查id=1
,您可以执行以下操作
SELECT 1 as id
FROM table_name
WHERE find_in_set('1', replace(myfield, '"', '')) > 0
您可以在代码中像这样使用它
$this->db->query("SELECT 1 as id
FROM table_name
WHERE find_in_set('1', replace(myfield, '\"', '')) > 0");
关于Mysql WHERE 字段包含 JSON 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15001889/