-1

如何在活动记录中的 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 是
222,111,444
333,444

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

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

4

2 回答 2

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");

希望它会有所帮助!

于 2013-03-19T08:58:09.517 回答
0

您以错误的方式使用它。标签后必须有一个空格

$this->db->where('tag >=',$date1);
$this->db->where('tag <=',$date2);
于 2013-03-19T10:12:49.833 回答