如何编写我们在mysql中使用的普通查询,我们在codeigniter中使用模型可以帮助我。
$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();
答案 0 :(得分: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();