为 codeigniter 的嵌套 select 编写 activerecord 时出现问题

having an issue writing activerecord for nested select for codeigniter

我正在尝试将嵌套查询转换为 codeigniter 上的活动记录。

我的查询:

SELECT COUNT(*) AS count, snapshot_id, snapshot_guid, image, subject, name, brands.brand_id, brand_guid, date_sent FROM
        (SELECT snapshot_id, snapshot_guid, snapshots.brand_id, image, subject, snapshots.status, date_sent 
        FROM snapshots INNER JOIN brands ON snapshots.brand_id = brands.brand_id 
        WHERE (snapshots.status = 1 AND brands.status = 1) AND DATE(date_sent) = '2015-2-9'
        ORDER BY date_sent DESC) snapshots
    INNER JOIN brands
    ON snapshots.brand_id = brands.brand_id
    GROUP BY snapshots.brand_id
    ORDER BY date_sent DESC;

它产生了正确的结果,但似乎无法使活动记录正常工作。

这是我目前的情况:

if(is_null($date))
      $date = date('Y-m-d');

    /* sub query */
    $this->db->select('snapshot_id, snapshot_guid, image, snapshots.brand_id, subject, snapshots.status, date_sent');
    $this->db->from('snapshots');
    $this->db->join('brands', 'snapshots.brand_id = brands.brand_id', 'inner');
    $this->db->where('snapshots.status', 1);
    $this->db->where('brands.status', 1);
    $this->db->where('DATE(date_sent)', $date);
    $this->db->order_by('date_sent', 'desc');
    $this->db->get();
    $sub = $this->db->last_query();

    $this->db->select('count(*) as count, snapshot_id, snapshot_guid, image, name, snapshots.brand_id, brand_guid, date_sent')
    //$this->db->from($sub, null, false);
    $this->db->join('brands', 'snapshots.brand_id = brands.brand_id', 'inner');
    $this->db->where('brands.status', 1);
    $this->db->group_by('snapshots.brand_id');
    $this->db->order_by('date_sent', 'desc');
    $this->db->limit($count);
    $query = $this->db->get($sub.' as snapshots');
    return $query->result();

错误在这里:$query = $this->db->get($sub .'as snapshots');

它正在子查询中添加一个额外的撇号。例如:

FROM (`SELECT` `snapshot_id`, `snapshot_guid`, `image`, `snapshots`.`brand_id`, `subject`, `snapshots`.`status`, `date_sent` FROM` (`snapshots`) INNER JOIN `brands` ON `snapshots`.`brand_id` = `brands`.`brand_id` WHERE `snapshots`.`status` = 1 AND `brands`.`status` = 1 AND DATE(date_sent) = '2015-2-9' ORDER BY `date_sent` desc as snapshots) INNER JOIN `brands` ON `snapshots`.`brand_id` = `brands`.`brand_id` WHERE `brands`.`status` = 1 GROUP BY `snapshots`.`brand_id` ORDER BY `date_sent` desc LIMIT 10

如您所见,SELECT 在后撇号

我建议你先group your code。编写另一个函数 subQuery 并从那里执行您的 mainQuery。

示例:

Class Example_model Extends CI_Model
{
   function subQuery()
   {
      $query = "SELECT value_2 FROM other_table WHERE id = 2";
      $q = $this->db->query($query);
      $this->mainQuery($q->result());
   } 
   function mainQuery($subQuery)
   {
     $query = "UPDATE some_table SET value_1 = "'.$subQuery.'" WHERE id = 2";
     $this->db->query($query);
   }
}

看完CodeIgniter User Guide后表示:

$this->db->get();

Runs the selection query and returns the result. Can be used by itself to retrieve all records from a table

我认为您必须在引号内包含 $sub 变量。