2017-01-15 273 views
-2
$this->db->select('*'); 
$this->db->from('messages'); 
$this->db->where('user_id_get', $data['users_id']); 
$this->db->group_by('user_id_get'); 
//$this->db->order_by('id','DESC'); This doesn't work 
$data["Query"] = $this->db->get(); 

如何在我的查詢(codeigniter)中使用order_by和group_by在這裏?order by by group by

+0

請參閱下面的鏈接[http://stackoverflow.com/questions/19943388/codeigniter-group-by-with-order-by-not-working-as-expected](http://stackoverflow.com/questions/ 19943388/codeigniter-group-by-by-by-working-as-expected) – msk

+0

好的,但是我的查詢是什麼? –

+0

身份證不在您的羣組中,它如何通過它進行排序? – Hogan

回答

0

我並不熟悉codeigniter中的sql處理,但groupBy user_id_getselect *是什麼意思?

如果多個行具有相同的user_id_get值進行彙總並僅由其中一個表示,那麼除user_id_get之外的其他屬性具有哪些值呢?實際上,只有一個分組記錄可以被採用,一些DB系統採用第一個,其他DB系統(和SQL標準)甚至禁止使用除group by子句中提到的那些之外的其他屬性。舉例來說,這個discussion at SO

因此,如果只有一組記錄一個取,這是什麼則意味着排序比按組的屬性之外的屬性,因爲這將意味着可能隨機選擇的值進行排序的,對不對?

所以,如果你想得到一個特定用戶的所有消息(如表達式where('user_id_get', $data['users_id']所示),那麼group by user_id_get是誤導性的;我會忽略該組,並僅使用該命令。

希望我明白你的要求,並希望它有幫助。