douyunjiaok300404 2015-06-16 07:32
浏览 67
已采纳

关联数组转换为Codeigniter中的OR子句

using CI I'm trying to set up query with three parameters: user_id, content_type and content_id in which content_id is array of IDs.

So I need to get up with something like this:

SELECT name FROM content WHERE user_id = 2 AND content_type = file AND (content_id = 4 OR content_id = 5 OR content_id = 7 ...).
  1. question: is this right query to get all the content_ids?
  2. does CI have any function where I can say

        $this->db->select('name');
        $this->db->from('content');
        $this->db->where('user_id', $user_id);
        $this->db->where('content_ids', $content_ids);
        $this->db->where('content_type', $content_type);
    

    just so that middle where clause is actually where with nested ORs in between and its filled with associative array ($content_ids)?

Thank you for answers.

EDIT: I have found a solution: where_in command does exactly what I need.

  • 写回答

1条回答 默认 最新

  • dongpeng0127 2015-06-16 08:42
    关注

    You can use IN clause to replace many OR conditions

    So your query within Active Records looks like

    $content_ids = array(4,5,7);
    $where = array('user_id' => $user_id,'content_type' => $content_type);
    $this->db->select('name');
    $this->db->from('content');
    $this->db->where($where);//created array of where clause
    $this->db->where_in('content_type', $content_ids);
    return $this->db->get()->result_array();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵