Active record in codeigniter

巧了我就是萌 提交于 2020-01-15 05:29:29

问题


Anybody can convert this query to active record codeigniter???

 SELECT  b.name,
    SUM(CASE WHEN c.size = 'S' THEN 1 ELSE 0 END) S,
    SUM(CASE WHEN c.size = 'M' THEN 1 ELSE 0 END) M,
    SUM(CASE WHEN c.size = 'L' THEN 1 ELSE 0 END) L,
    SUM(CASE WHEN c.size = 'XL' THEN 1 ELSE 0 END) XL
   FROM    orderTB a
    INNER JOIN productTB b
        ON a.id_product = b.id_shirt
    INNER JOIN sizeTB c
        ON a.id_size = c.id_size
GROUP BY b.name

i've tried like this

  function get()
 {
  $this->db->select("b.name,SUM(CASE WHEN c.size ='S' THEN 1 ELSE 0 END) as S,SUM(CASE    WHEN c.size = 'M' THEN 1 ELSE 0 END) as M,SUM(CASE WHEN c.size = 'L' THEN 1 ELSE 0 END) as   L,SUM(CASE WHEN c.size = 'XL' THEN 1 ELSE 0 END) as XL");

 $this->db->from('order');
 $this->db->join('ukuran','order.id_size=ukuran.id_size');
 $this->db->where('date',$date);
 return $this->db->get();
 }

but its wrong


回答1:


Just try this one and let me know,

$this->db->select("b.name,SUM(CASE WHEN c.size = 'S' THEN 1 ELSE 0 END) S,SUM(CASE WHEN c.size = 'M' THEN 1 ELSE 0 END) M,SUM(CASE WHEN c.size = 'L' THEN 1 ELSE 0 END) L,SUM(CASE WHEN c.size = 'XL' THEN 1 ELSE 0 END) XL", FALSE );
$this->db->from('orderTB a');
$this->db->join('productTB b','a.id_product = b.id_shirt','inner');
$this->db->join('sizeTB c','a.id_size = c.id_size','inner');
$this->db->group_by('b.name');

From document here

$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.

EDIT

Just add FALSE in your select statement

function get() {
    $this->db->select("b.name,SUM(CASE WHEN c.size ='S' THEN 1 ELSE 0 END) as S,SUM(CASE    WHEN c.size = 'M' THEN 1 ELSE 0 END) as M,SUM(CASE WHEN c.size = 'L' THEN 1 ELSE 0 END) as   L,SUM(CASE WHEN c.size = 'XL' THEN 1 ELSE 0 END) as XL",FALSE);

    $this->db->from('order');
    $this->db->join('ukuran','order.id_size=ukuran.id_size');
    $this->db->where('date',$date);
    return $this->db->get();
}


来源:https://stackoverflow.com/questions/14110064/active-record-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!