在笨

2013-03-23 48 views
1

使用順序按這是我正常的MySQL查詢:在笨

$sql = "SELECT * FROM item order by "; 
if(my_condition) 
{ 
     $sql. = "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')"; 

} 
else 
{ 
    $sql .= "category asc"; 
} 

我需要它在CI中的活動記錄。我試着在下列方式:

 if(my_condition) 
      { 
       $this->db->order_by("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')"); 

      } 
      else 
      { 
       $this->db->order_by("category", "asc"); 
      } 

但是,我得到這個錯誤::

Error Number: 1064 
    You have an error in your SQL syntax; check the manual that corresponds to your   
    MySQL server version for the right syntax to use near 'Couch'`, `'Bed'`, `'Desk` & 
    Office', `'Bike` & Scooter', `'Tools'`, `'Leisure'`,' at line 6 


    Filename: C:\xampp\htdocs\straatjutter_service\system\database\DB_driver.php 

Line Number: 330 

如何解決呢?其實我需要以上述格式排序我的產品類別。這可以在普通的mysql查詢中完成。但是,在活動記錄中做這件事會感到困惑。

+0

這似乎是正確的。嘗試刪除列名前後的波形。 – verisimilitude 2013-03-23 11:22:01

+1

omfkg是這個數據庫字段名稱*玩具和遊戲*? – sbaaaang 2013-03-23 11:24:53

+1

我在想同樣的 – 2013-03-23 11:25:45

回答

3

試試這個::

  $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'); 
      } 
1

更好地使用這種方式

$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

'$ ids = 1,3,2,4'是無效的PHP。 – FrankerZ 2016-08-11 07:25:03

+0

什麼是我是$ ids =「1,3,2,4」; – 2016-08-11 09:11:21

+0

然後你應該更新你的答案。 – FrankerZ 2016-08-11 09:20:46