0

将mysql查询转换为codeigniter查询

当前查询:


$query = "SELECT a.*, b.cost AS future_cost, b.start_date AS future_date
          FROM (
            SELECT * FROM (
              SELECT * FROM outbound_routes 
              ORDER BY start_date
            ) x 
          GROUP BY pattern
          ) a
          LEFT JOIN (
            SELECT * FROM outbound_routes 
            ORDER BY start_date
          ) b 
          ON a.pattern = b.pattern AND a.id != b.id
          GROUP BY a.pattern";

$result=$this->db->query($query);

在粗体查询中,我想附加搜索变量,例如

 $this->db->where('pattern','1');

任何帮助和想法将不胜感激。

4

2 回答 2

0

不幸的是,我认为不可能使用 codeigniters活动记录类将 where 子句附加到现有 SQL 中。活动记录类只能处理表之间的简单连接。

要将其附加到现有查询,您可以使用查询绑定将变量传递给查询。

$query = "SELECT a.*, b.cost AS future_cost, b.start_date AS future_date
          FROM (
            SELECT * FROM (
              SELECT * FROM outbound_routes 
              ORDER BY start_date
            ) x 
          GROUP BY pattern
          ) a
          LEFT JOIN (
            SELECT * FROM outbound_routes 
            ORDER BY start_date
          ) b 
          ON a.pattern = b.pattern AND a.id != b.id
          GROUP BY a.pattern
          WHERE pattern = ?";

$result = $this->db->query($query, array(1));
于 2013-09-30T06:55:52.603 回答
0

试试这个

$this->db->select('select a.*, b.cost AS future_cost, b.start_date AS future_date');
$this->db->from('(select * from   (select * from outbound_routes order by start_date) x group by pattern) a');
$this->db->join('(select * from outbound_routes order by start_date) b', 'a.pattern = b.pattern and a.id != b.id','left ');
$this->db->where('a.pattern','1');
$this->db->group_by("a.pattern"); 
$result=$query = $this->db->get();

活动记录

或查看CodeIgniter 子查询

于 2013-09-30T06:56:24.197 回答