0

如何以我们在codeigniter中的模型中使用的形式编写我们在mysql中使用的普通查询有人可以帮助我。

$sql = "SELECT gender,
               SUM(CASE WHEN Handedness = 'Left-handed' THEN 1 ELSE 0 END) left_handed,
               SUM(CASE WHEN Handedness = 'Right-handed' THEN 1 ELSE 0 END) right_handed,
               COUNT(*) total
          FROM Table1
         GROUP BY gender WITH ROLLUP";
$data = $this->db->query($sql)->result_array();

如何以我们在 codeigniter 中的模型中使用的形式编写它

$this->db->select('financial_year');
        $this->db->from('historical_data');
        $this->db->where("financial_year BETWEEN '$from' AND '$to'");
        $this->db->group_by('financial_year');
        $query = $this->db->get();
        $result = $query->result();
4

1 回答 1

0

看看它是否有效。

$this->db->select("gender, SUM(CASE WHEN Handedness = 'Left-handed' THEN 1 ELSE 0 END) left_handed, SUM(CASE WHEN Handedness = 'Right-handed' THEN 1 ELSE 0 END) right_handed, COUNT(*) total", false)->group_by('gender WITH ROLLUP')->get('Table1')->result_array();

根据您遇到的错误进行编辑:

$this->db->select("question_no, SUM(CASE WHEN organization_answer_rating = '1' THEN 1 ELSE 0 END) 1, SUM(CASE WHEN organization_answer_rating = '2' THEN 1 ELSE 0 END) 2, COUNT(*) as total", false)->group_by('question_no WITH ROLLUP')->get('questionnaire')->result_array();
于 2013-09-13T11:15:22.180 回答