2013-02-20 27 views
1

如何以Codeigniter风格编写以下查询。以codeigniter风格编写联合查询

SELECT COUNT(`id`) AS reccount 
    FROM 
    (SELECT `id` FROM table1 
    WHERE tid= '101' AND `status` = 1 
    UNION ALL 
    SELECT `id` FROM table2 
    WHERE tid= '101' AND `status` = 1 
    UNION ALL 
    SELECT `id` FROM table3 
    WHERE tid= '101' AND `status` = 1) t 

我用下面的方法来执行它。

这是唯一正确的方法还是您有任何改进建议?

$q = $this->db->query(SELECT COUNT(`id`) AS reccount 
         FROM 
         (SELECT `id` FROM table1 
         WHERE tid= '101' AND `status` = 1 
         UNION ALL 
         SELECT `id` FROM table2 
         WHERE tid= '101' AND `status` = 1 
         UNION ALL 
         SELECT `id` FROM table3 
         WHERE tid= '101' AND `status` = 1) t "); 
+0

可能重复http://stackoverflow.com/questions/2040655/ union-query-with-codeigniters-active-record-pattern) – 2014-09-30 19:19:44

回答

-1
function get_merged_result($ids){     
    $this->db->select("column"); 
    $this->db->distinct(); 
    $this->db->from("table_name"); 
    $this->db->where_in("id",$model_ids); 
    $this->db->get(); 
    $query1 = $this->db->last_query(); 

    $this->db->select("column2 as column"); 
    $this->db->distinct(); 
    $this->db->from("table_name"); 
    $this->db->where_in("id",$model_ids); 

    $this->db->get(); 
    $query2 = $this->db->last_query(); 
    $query = $this->db->query($query1." UNION ".$query2); 

    return $query->result(); 
} 
+0

在这里,你正在运行2个查询只是为了获得SQL输出并将它们与union结合起来?不妨像OP那样在第一时间写出查询。 – dakdad 2013-02-20 12:24:07

+1

Nop,你可以通过阅读这篇文章得到更多的细节,http://codesamplez.com/database/codeigniter-activerecord – Shaolin 2013-02-20 12:30:13

+1

问题是,'$ this-> db-> get();'是要执行查询。因此,您正在运行2个查询,只是为了获得第三个SQL语句!你可以把它写成一个字符串并传递给'$ this-> db-> query($ sql);'函数。 – dakdad 2013-02-21 00:16:59

1

您可以使用CI生成一个联合查询。但是,最新版本比以前更加困难。

DB有一个名为_compile_select的方法,在以前的CI版本中它是公开的,但是现在它受到保护,因此您不能从控制器调用$this->db->_compile_select()。为了正确地做到这一点人们可以:

  1. 创建自定义的类装载器,以便能够扩展核心/数据库类(即负载MY_DB_active_record代替CI_DB_active_record)。
  2. 创建自定义的ActiveRecord类,只有一个方法:

    public function compile_select() { 
        return $this->_compile_select(); 
    } 
    
  3. 在你的控制器,创造一切必要的查询,使用我们的公共方法编译成一个字符串数组compile_select()

  4. 阵列加入到单个查询:'(' . implode(') UNION (', $queries) . ')'。您也可以将其封装到自定义AR类中的单独方法中。
13

由于CodeIgniter 3在Active Record中引入了函数get_compiled_select(),该函数在不实际执行查询的情况下给出查询字符串。

这允许@MDeSilva方法使用更少的资源,被适配为:

function get_merged_result($ids){     
    $this->db->select("column"); 
    $this->db->distinct(); 
    $this->db->from("table_name"); 
    $this->db->where_in("id",$model_ids); 
    $query1 = $this->db->get_compiled_select(); // It resets the query just like a get() 

    $this->db->select("column2 as column"); 
    $this->db->distinct(); 
    $this->db->from("table_name"); 
    $this->db->where_in("id",$model_ids); 
    $query2 = $this->db->get_compiled_select(); 

    $query = $this->db->query($query1." UNION ".$query2); 

    return $query->result(); 
} 
[UNION与笨的活动记录图案查询](的
+0

如果你打算使用这个,请确保你的表格列都是相同的序列和名称。 – 2017-08-24 06:04:02