Codeigniter在哪里和替换子查询

时间:2013-03-19 08:34:33

标签: codeigniter activerecord subquery where-clause

如何在活动记录中的codeigniter中写入以下查询?

SELECT
  time AS date,
  user AS name
FROM b_table
WHERE id IN(SELECT
          a
        FROM a_table
        WHERE group = $group
        AND tag >= '$date1'
        AND tag <= $date2')

这就是我提出的问题,但它不起作用。

$this->db->select('a');
$this->db->from('a_table');
$this->db->where('group',$group);
$this->db->where('tag>=',$date1);
$this->db->where('tag<=',$date2);

$subQuery = $this->db->_compile_select();
$this->db->_reset_select();

$this->db->select('time AS date, user AS name');
$this->db->from('b_table');
$this->db->where_in($subQuery);

我的问题是(a)从选择一个是需要替换的记录,如替换空格为逗号或者我错了:(,
另一个我用str_replace但仍然错了,

  
    

示例数据      a从选择一个是     222111444个
     333444     

       555666个
    替换为222,111,444,333,444,555,666

  

即使我是codeigniter的新手 感谢任何帮助,抱歉我的英语。
感谢。

2 个答案:

答案 0 :(得分:1)

试试这个:

 ->where() 

支持将任何字符串传递给它,它将在查询中使用它。

所以上面就像是:

 $this->db->select('time AS date,user AS name')->from('b_table');
 $this->db->where_not_in('id', "SELECT a FROM a_table WHERE group = $group AND tag >= '$date1' AND tag <= $date2");
希望它会有所帮助!

答案 1 :(得分:0)

你正在以错误的方式使用它。标签后面必须有空格

$this->db->where('tag >=',$date1);
$this->db->where('tag <=',$date2);