【问题标题】:Combination of AND and OR clausesAND 和 OR 子句的组合
【发布时间】:2013-07-25 07:28:32
【问题描述】:

我想在 CodeIgniter 中实现下面的 mysql 语句。

select * from table_name 
         where deleted = 0 and (id = "18" or name = "efgh" or imei = "1244");

我在 CodeIgniter 中写了以下语句:

$this->db->where('deleted', 0);
$this->db->or_where('id', $this->table_name->id);
$this->db->or_where('imei', $this->table_name->imei);
$this->db->or_where('name', $this->table_name->name);
$result= $this->db->get('table_name')->result();

但这两个语句给出了不同的输出。谁能指出 CodeIgniter 语句中的错误?谢谢。

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    请使用这个:-

       $where = "deleted='0' AND (id='18' OR name='efgh' OR imei='1244')";
       $this->db->where($where);
       $result= $this->db->get('table_name')->result();
    

    更多详情您可以访问:-

    http://ellislab.com/codeigniter/user-guide/database/active_record.html

    【讨论】:

      【解决方案2】:

      我会用

      $this->db->where('deleted', 0);
      $this->db->where("(id='18' OR name='efgh' OR imei='1244')", NULL, FALSE);
      $result = $this->db->get('table_name')->result();
      

      干杯,

      【讨论】:

        【解决方案3】:

        我以前也遇到过同样的问题。

        我无法用$this->db 函数计算出来。

        我修改system/database/DB_active_rec.php_compile_select()函数

        原文: 1746 行

        if (count($this->ar_like) > 0) {
          if (count($this->ar_where) > 0) {
            $sql .= "\nAND ";
          }
          $sql .= implode("\n", $this->ar_like);
        }
        

        我的更改:

        if (count($this->ar_like) > 0) {
          if (count($this->ar_where) > 0) {
            $sql .= "\nAND ("; // open parenthesis for where and like command
          }
          $sql .= implode("\n", $this->ar_like);
          if (count($this->ar_where) > 0) {
            $sql .= "\n)"; // close parenthesis and sample sql text > WHERE column = x AND (LIKE QUERIES)
          }
        }
        

        【讨论】:

          【解决方案4】:

          是的,activerecord 使用有问题,或者,如果您使用 activerecord,它将不会为 or 条件添加开始和结束括号,然后比较,您的查询将是这样的

          select * from table_name where deleted = 0 and id="18" or name = "efgh" or imei = "1244";
          

          使用它

          $where = 'deleted = 0 and (id="18" or name = "efgh" or imei = "1244")';
          
          $this->db->where($where);
          

          【讨论】:

          • 我们可以用$this->db->where$this->db->or_where 写$where 吗?我不喜欢字符串查询。
          • 您好,查看这篇文章可以帮助您同时使用这两种方法stackoverflow.com/questions/11186491/…
          • 它们没用。他们也修改系统文件。我正在搜索没有修改系统文件的代码。
          【解决方案5】:
          There is two way to do the same thing.
          first 
          $condition = "deleted='0' AND (id='18' OR name='efgh' OR imei='1244')";
          $this->db->where($condition);
          $result= $this->db->get('table_name')->result();
           and 
          write your costume query 
          
          $this->db->query('YOUR QUERY HERE');
          

          希望对你有所帮助。

          【讨论】:

            猜你喜欢
            • 2011-08-18
            • 2017-11-16
            • 1970-01-01
            • 2015-08-26
            • 2021-02-02
            • 1970-01-01
            • 1970-01-01
            • 2018-06-03
            • 1970-01-01
            相关资源
            最近更新 更多