Using Order By in codeigniter

后端 未结 2 1892
孤城傲影
孤城傲影 2021-01-24 10:34

This is my normal mysql query:

$sql = \"SELECT * FROM item order by \";
if(my_condition)
{
       $sql. = \"FIELD(`category`, \'Auto & Accessories\', \'Couch         


        
相关标签:
2条回答
  • 2021-01-24 10:52

    Better use this way

    $ids = "1,3,2,4";
    $qry ="SELECT * FROM (`destination`) WHERE `id` IN ($ids) ORDER BY `FIELD` ( id, $ids )";
    $result = $this->db->query($qry);
    return $result->result();
    
    0 讨论(0)
  • 2021-01-24 11:06

    Try this::

                $this->db->select('i.id, condition, description, created_at, updated_at, category, latitude, longitude, status, d.device_token, d.facebook, d.preferred, d.email, d.phone');
                $this->db->select("CONCAT('$url', `image_url_original`) AS `image_url_original`", false);
                $this->db->select("CONCAT('$url', `image_url_mid`) AS `image_url_mid`", false);
                $this->db->select("CONCAT('$url', `image_url_thumb`) AS `image_url_thumb`", false);
                $this->db->select("FIELD(`category`, 'Auto & Accessories', 'Couch', 'Bed', 'Desk & Office', 'Bike & Scooter', 'Tools', 'Leisure', 'Iron & Wood', 'Cabinet', 'Kitchen & Accessories', 'Refrigerator & Appliances', 'Toys & Games', 'Chair', 'Table', 'Garden & Terrace', 'TV, HIFI & Computers', 'General Item') AS `sort_col`", false);
    
                $this->db->from('item as i');
                $this->db->join('device as d', 'i.device_id = d.device_token');
    
                $this->db->where('created_at > DATE_SUB(UTC_TIMESTAMP(), INTERVAL 24 HOUR)');
                $this->db->where('i.status', 'shared');
    
                if($language === 'nl_NL') {
                    $this->db->order_by('sort_col', 'asc');
                } else {
                    $this->db->order_by('category', 'asc');
                } 
    
    0 讨论(0)
提交回复
热议问题