11

I want to sort my database values in descending order using this query below in my model. However, it doesn't completely sort in descending order all the values in the database but when ascending is used, it works well.

function sort_all_courses_desc($tennant_id)
{
    $this->db->select('*');
    $this->db->where('tennant_id',$tennant_id);
    $this->db->order_by("course_name","desc");
    $this->db->from('courses');
    $query=$this->db->get();
    return $query->result();
}
4

3 回答 3

11

将 from 放在 where 之前,并将 order_by 放在最后:

$this->db->select('*');
$this->db->from('courses');
$this->db->where('tennant_id',$tennant_id);
$this->db->order_by("UPPER(course_name)","desc");

或尝试二进制:

ORDER BY BINARY course_name DESC;

您应该在 codeigniter 上手动添加以进行二进制排序。

并设置“course_name”字符列。

如果对字符类型列使用排序,则通常以不区分大小写的方式进行排序。

课程表中的结构数据类型是什么?

如果您感到沮丧,您可以使用 PHP 放入数组并返回:

使用 natcasesort 按“自然顺序”排序:(参考:http ://php.net/manual/en/function.natcasesort.php )

您来自数据库的数组作为示例$array_db = $result_from_db::

$final_result = natcasesort($array_db);

print_r($final_result);
于 2013-03-08T01:33:02.113 回答
5

将该行 $this->db->order_by("course_name","desc"); 放在查询的顶部。喜欢

$this->db->order_by("course_name","desc");$this->db->select('*');
$this->db->where('tennant_id',$tennant_id);
$this->db->from('courses');
$query=$this->db->get();
return $query->result();
于 2016-12-02T06:27:41.837 回答
1
$this->db1->where('tennant_id', $tennant_id);
$this->db1->order_by('id', 'DESC');
return $this->db1->get('courses')->result();
于 2019-11-11T06:04:39.350 回答