2

任何人都可以将此查询转换为活动记录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

我试过这样

  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();
 }

但它错了

4

1 回答 1

10

试试这个,然后告诉我,

$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');

这里的文件

$this->db->select() 接受可选的第二个参数。如果您将其设置为 FALSE,CodeIgniter 将不会尝试使用反引号保护您的字段或表名。如果您需要复合选择语句,这很有用。

编辑

只需添加FALSE您的选择语句

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();
}
于 2013-01-01T11:50:39.697 回答