2016-08-16 9 views
0

私は現在、Codeigniterのクエリに取り組んでいます。ここでは、たくさんの結果を得ようとしています。しかし、同じクエリ内でgroup_byとorder_byの両方を使用すると、order_byは無効になり、何らかの理由で適用されません。Codeigniter group_byとOrder_byの競合

クエリは以下の通りです:

$this->db->select('q.questionNumber, qa.qaId')->from('questions q'); 
$this->db->join('questionAnswers qa', 'qa.questionId = q.questionId'); 
$this->db->where('customerId', $customerId); 
$this->db->order_by('q.questionNumber', 'desc'); 
$this->db->group_by('q.subject'); 
$query = $this->db->get(); 
$query->result(); 

答えて

1

使用あなたはあなたのクエリのために、このソリューションを試すことができます:

$this->db->select('q.subject,COUNT(q.questionNumber), COUNT(qa.qaId)')->from('questions q'); 
$this->db->join('questionAnswers qa', 'qa.questionId = q.questionId'); 
$this->db->where('customerId', $customerId); 
$this->db->group_by('q.subject'); 
$this->db->order_by('q.questionNumber', 'desc'); 
$query = $this->db->get(); 
$query->result(); 

Nessaryは、集計機能を持つGroup By Clauseおよびその他のフィールドを持つクエリで選択行に列を追加します。

+0

@S_NoBEIはうまくいきますか? –

0

あなたはこれを使用してクエリを書くことができます:1

$this->db->select('q.questionNumber, qa.qaId')->from('questions q'); 
$this->db->select_max('q.questionNumber' , 'questionNumber'); // added this 
$this->db->join('questionAnswers qa', 'qa.questionId = q.questionId'); 
$this->db->where('customerId', $customerId); 
$this->db->order_by('q.questionNumber', 'desc'); 
$this->db->group_by('q.subject'); 
$query = $this->db->get(); 
$query->result(); 
+1

どのように問題を修正しますか? –

+0

基本的なSQLを使って質問を書いてください。おそらくcodeigniterがどのようにクエリにエラーを出しているのですか? –

0

を更新

$query = $this->db->query("SELECT ....."); 

$query->result();