我有一个问题.
当我尝试按特定值排序时,sql查询在codeigniter中不起作用
例子如下:
$this->db->select('*');
$this->db->from('Questions');
$this->db->where('Questions.Status !=', 0);
$this->db->order_by('IdQuestion', 'DESC');
$this->db->order_by('(CASE WHEN Status = 3 THEN 1 ELSE 2 END)', 'DESC'); //Here's wrong...
但是我没有收到有效的结果.
有人可以帮忙.
第二个order_by语句错误.
情况不正确.
解决方法:
您可以尝试解决此问题的方法:
<?php
$sub_query_from = '(SELECT Questions.*, (CASE WHEN Questions.Status = 3 THEN 1 ELSE 2 END) as questions_status from Questions WHERE Questions.Status != 0 ORDER BY IdQuestion DESC) as sub_questions';
$this->db->select('sub_questions.*');
$this->db->from($sub_query_from);
$this->db->order_by('sub_questions.questions_status', 'DESC');
$query = $this->db->get();
$result = $query->result();
echo "<per>";
print_r($result);
exit;
?>
希望它会有所帮助.