【问题标题】:mySQL search with datatables使用数据表进行 mySQL 搜索
【发布时间】:2017-08-17 05:54:11
【问题描述】:

我正在使用 DataTables 和 CodeIgntier 在 mySql 中搜索数据,

下面是控制器搜索循环和模型,

控制器搜索构建

    if (isset($searchValue) && $searchValue != '')
    {
        $searching = array();
        for ($i=0; $i<count($columns); $i++) //Loop search in all defined columns
        {
            $searching = $this->db->or_like($columns[$i], $searchValue);
        }
    }
    else
    {
        $searching = NULL;
    }

$this->model->get_myreports($this->session->userdata('user_id'), $searching);

模型函数,

public function get_myreports($user_id, $searching)
{
    $this->db->where('user_id', $user_id);
    $searching;
    return $this->db->get('reports')->result_array();
}

这又会导致下面的 sql 查询,

SELECT * FROM `reports` WHERE `report_id` LIKE '%sup%' ESCAPE '!' OR `report_name` LIKE '%sup%' ESCAPE '!' OR `report_submitted` LIKE '%sup%' ESCAPE '!' OR `report_total` LIKE '%sup%' ESCAPE '!' OR `report_status` LIKE '%sup%' ESCAPE '!' OR `report_received` LIKE '%sup%' ESCAPE '!' AND `user_id` = '4' ORDER BY `report_status` DESC, `report_id` LIMIT 10

现在的问题是,

它在进行搜索时显示所有用户报告,即使我告诉使用 user_id = 4 进行搜索,它也显示 user_id = 1 的报告,而仅在进行搜索时, 在正常的datatables 页面加载时,它会显示正确的 user_id = 4 报告,但问题仅在搜索查询中,

如何只搜索user_id = 4

谢谢,

【问题讨论】:

    标签: php mysql codeigniter datatables


    【解决方案1】:

    应该是这样的:

    public function get_myreports($user_id, $searching)
    {
       $this->db->->group_start()       
       ->or_like($searching)
       ->group_end()
       ->where('user_id', $user_id);
       return $this->db->get('reports')->result_array();
    }
    

    【讨论】:

    • 我快到了,但它创建了类似这样的东西,它只添加带有一个 AND........SELECT * FROM reports WHERE report_id LIKE '%sup% ' 逃脱 '!' ' OR report_received LIKE '%sup%' ESCAPE '!' AND ( ) AND user_id = '4' ORDER BY report_status DESC, report_id LIMIT 10
    • $searching = $this-&gt;db-&gt;or_like($columns[$i], $searchValue);应该是一个数组,然后or_like会做括号的分组任务:$searching[$columns[$i]] = $searchValue;
    • 现在还有一个问题,当我删除所有关键字进行搜索时,现在它在数据表中显示错误,因为搜索无法完成,.....SELECT * FROM ec_reports WHERE ( LIKE '% %' ESCAPE '!' ) AND user_id = '4' ORDER BY report_status DESC, report_id LIMIT 10
    • 添加IF 条件,例如if($searching != NULL) { ... } else{ .. only where user_id clause...no grouping is required}
    【解决方案2】:

    试试这个。为整个或大小写添加括号

    SELECT * FROM `reports` WHERE (`report_id` LIKE '%sup%' ESCAPE '!' OR `report_name` LIKE '%sup%' ESCAPE '!' OR `report_submitted` LIKE '%sup%' ESCAPE '!' OR `report_total` LIKE '%sup%' ESCAPE '!' OR `report_status` LIKE '%sup%' ESCAPE '!' OR `report_received` LIKE '%sup%' ESCAPE '!') AND (`user_id` = '4') ORDER BY `report_status` DESC, `report_id` LIMIT 10
    

    【讨论】:

      【解决方案3】:

      可能是这样的查询。在搜索中添加 () 括号。

      SELECT * FROM `reports` WHERE (`report_id` LIKE '%sup%' ESCAPE '!' OR `report_name` LIKE '%sup%' ESCAPE '!' OR `report_submitted` LIKE '%sup%' ESCAPE '!' OR `report_total` LIKE '%sup%' ESCAPE '!' OR `report_status` LIKE '%sup%' ESCAPE '!' OR `report_received` LIKE '%sup%' ESCAPE '!') AND `user_id` = '4' ORDER BY `report_status` DESC, `report_id` LIMIT 10
      

      【讨论】:

      • 好的,那么现在我需要看看如何在 CI 框架查询中构建括号
      • $this->model->get_myreports($this->session->userdata('user_id'), '('.$searching.')');使用连接。
      【解决方案4】:
      $this->model->get_myreports($this->session->userdata('user_i‌​d'), '('.$searching.')');
      
      SELECT * FROM `reports` WHERE (`report_id` LIKE '%sup%' ESCAPE '!' OR `report_name` LIKE '%sup%' ESCAPE '!' OR `report_submitted` LIKE '%sup%' ESCAPE '!' OR `report_total` LIKE '%sup%' ESCAPE '!' OR `report_status` LIKE '%sup%' ESCAPE '!' OR `report_received` LIKE '%sup%' ESCAPE '!') AND `user_id` = '4' ORDER BY `report_status` DESC, `report_id` LIMIT 10
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-03
        • 2019-11-16
        • 1970-01-01
        • 1970-01-01
        • 2012-01-25
        • 1970-01-01
        • 2017-03-11
        • 1970-01-01
        相关资源
        最近更新 更多