需要帮助在codeigniter中转换这段代码

时间:2013-04-24 06:49:05

标签: sql codeigniter

任何人都可以帮我转换吗?

SELECT  id,ospos_people.first_name,ospos_people.last_name,customer_id,is_draft 
from
(SELECT  
          ospos_quotations.id,
          ospos_quotations.customer_id, 
          ospos_quotations.is_draft,
          ospos_quotations.date 
 from     ospos_quotations, ospos_quotation_items
 where  ospos_quotations.id= ospos_quotation_items.quotation_id 
 and ospos_quotation_items.line=1) AS T 
LEFT OUTER JOIN ospos_people on T.customer_id = ospos_people.person_id

2 个答案:

答案 0 :(得分:0)

也许:

$sql = 'SELECT  id,ospos_people.first_name,ospos_people.last_name,customer_id,is_draft 
from
(SELECT  
          ospos_quotations.id,
          ospos_quotations.customer_id, 
          ospos_quotations.is_draft,
          ospos_quotations.date 
 from     ospos_quotations, ospos_quotation_items
 where  ospos_quotations.id= ospos_quotation_items.quotation_id 
 and ospos_quotation_items.line=1) AS T 
LEFT OUTER JOIN ospos_people on T.customer_id = ospos_people.person_id';

$this->db->query($sql);


// OR 

$this->db
    ->select('id,ospos_people.first_name,ospos_people.last_name,customer_id,is_draft')
    ->from('(SELECT  
          ospos_quotations.id,
          ospos_quotations.customer_id, 
          ospos_quotations.is_draft,
          ospos_quotations.date 
 from     ospos_quotations, ospos_quotation_items
 where  ospos_quotations.id= ospos_quotation_items.quotation_id 
 and ospos_quotation_items.line=1) AS T', false)
    ->join('ospos_people', 'T.customer_id = ospos_people.person_id', 'left outer');

答案 1 :(得分:0)

我已将您的查询转换为更优化,如此

SELECT
  id,
  ospos_people.first_name,
  ospos_people.last_name,
  customer_id,
  is_draft
from (SELECT
    ospos_quotations.id,
    ospos_quotations.customer_id,
    ospos_quotations.is_draft,
    ospos_quotations.date
      from ospos_quotations
    LEFT JOIN ospos_quotation_items
      ON ospos_quotations.id = ospos_quotation_items.quotation_id
      where ospos_quotation_items.line = 1) AS T
  LEFT OUTER JOIN ospos_people
    on T.customer_id = ospos_people.person_id

在这个内部查询中,我使用了join而不是FROM table1,table2

您可以像这样转换

您可以使用codeigniter的子查询方式为此执行此操作 目的,你将不得不破解codeigniter。像这样。去 system / database / DB_active_rec.php删除public或protected 这些函数中的关键字

public function _compile_select($select_override = FALSE)
public function _reset_select()

现在子查询写入可用现在这里是带有活动记录的查询

function myquery()
{
    $select =   array(
                    'ospos_quotations.id',
                    'ospos_quotations.customer_id', 
                    'ospos_quotations.is_draft',
                    'ospos_quotations.date'
                );
    $this->db->select($select);
    $this->db->from('ospos_quotations');
    $this->db->join('ospos_quotation_items','ospos_quotations.id= ospos_quotation_items.quotation_id ','left');
    $this->db->where('ospos_quotation_items.line',1);
    $subQuery = $this->db->_compile_select(); // get query string

    $this->db->_reset_select(); // reset the object to write new query

    unset($select);

    $select =   array(
                    'id',
                    'ospos_people.first_name',
                    'ospos_people.last_name',
                    'customer_id,is_draft' 
                );
    $this->db->select($select);             
    $this->db->from("($subQuery) AS T");
    $this->db->join('ospos_people','T.customer_id = ospos_people.person_id ','left outer');
    return $this->db->get()->result();
}

事情已经完成了。干杯!!! 注意:使用子查询时,必须使用

$this->db->from('table_name')

而不是

$this->db->get('table_name')

运行查询。

编辑:

这是另一种方式。只需使用$this->db->query($query)功能

即可
function myquery()
{
    $query  =   "   SELECT
                      id,
                      ospos_people.first_name,
                      ospos_people.last_name,
                      customer_id,
                      is_draft
                    from (SELECT
                        ospos_quotations.id,
                        ospos_quotations.customer_id,
                        ospos_quotations.is_draft,
                        ospos_quotations.date
                          from ospos_quotations
                        LEFT JOIN ospos_quotation_items
                          ON ospos_quotations.id = ospos_quotation_items.quotation_id
                          where ospos_quotation_items.line = 1) AS T
                      LEFT OUTER JOIN ospos_people
                        on T.customer_id = ospos_people.person_id";
    return $this->db->query($query)->result();  
}