简体   繁体   English

Codeigniter where_in JSON 列

[英]Codeigniter where_in JSON column

i need a Codeigniter query to get results from a JSON column MYSQL table.我需要一个 Codeigniter 查询来从 JSON 列 MYSQL 表中获取结果。

Sample column data bellow.示例列数据如下。

["1","2","6","4","3","5","7","9","8"]

Currently im using bellow code for this but it's not working.目前我为此使用波纹管代码,但它不起作用。

$DOCUMENTS = $this->db->select('*')->where('JSON_CONTAINS(BELONG) = '.$_POST['ID'].'')->get('SOLIDARITY_REQUESTED_FILES')->result_array();

If your JSON formatted as array like your data, you can concatenate the ID to the commas and quotes.如果您的 JSON 格式与您的数据一样为数组,您可以将 ID 连接到逗号和引号。 then TRIM the square bracket and find by LIKE statement.然后 TRIM 方括号并通过 LIKE 语句查找。

$concat = "CONCAT(',\"',TRIM(LEADING '[' FROM TRIM(TRAILING ']' FROM BELONG)),'\",')";
$theId = ',"'.$_POST['ID'].'",';
$DOCUMENTS = $this->db->select('*')->like($concat, $theId)->get('SOLIDARITY_REQUESTED_FILES')->result_array();

Hope this help.希望这有帮助。

Suppose i'm doing to get record where city_id should be in following list.假设我正在记录 city_id 应该在以下列表中的位置。 1,2,3,4,5,6,7,8 then following line you can add into your code 1,2,3,4,5,6,7,8 然后下一行你可以添加到你的代码中

    $city_id_list = "1,2,6,4,3,5,7,9,8";
    $this->db->where_in('city_id', $city_id_list);

JSON_CONTAINS function required two minimum arguments. JSON_CONTAINS函数至少需要两个参数。 check more details HERE这里查看更多详细信息

It is dangerous to add $_POST data into query.$_POST数据添加到查询中是危险的。 Codeigniter having input class to fetch data from POST request.具有input类的 Codeigniter 从POST请求中获取数据。

$id = $this->input->post('ID');

$DOCUMENTS = $this->db->select('*')->where("JSON_CONTAINS(BELONG, $id)>0")->get('SOLIDARITY_REQUESTED_FILES')->result_array();

Feel free if you need further guidance.如果您需要进一步的指导,请随意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM