mysql クエリを Code Igniter の Active Record 構文に取り込もうとしていますが、少し苦労しています。
クエリは、次の質問の結果です: Multiple mysql ORDER BY's for multidimensional orders/grouping
私は自分でクエリをフォーマットしようとしましたが、いくつかのエラーに取り組みましたが、どのように進めればよいかわかりません. get_compiled_select()
関数をDB_active_rec.php
自分自身に追加し、_reset_select()
保護されたものからパブリックに変更して、まったく実行できるようにする必要がありました。
推奨されるクエリは次のとおりです。
select
t.id,
t.group,
t.date,
t.comlete
from
YourTable t
left join
(select
m.group,
min(m.date) as mindate,
min(t.complete) as groupcomplete
from
YourTable m) mt on mt.group = t.group
order by
coalesce(mt.groupcomplete, t.complete),
coalesce(mt.mindate, t.date),
t.group,
t.complete,
t.date
私の翻訳は次のようになります (オリジナルには「where」節があり、「date」は実際には「due」であることに注意してください):
// Sub query
$this->db->select('m.group, min(m.due) as mindate, min(t.complete) as groupcomplete');
$this->db->from('task m');
$this->db->where('property', $property);
$subquery = $this->db->get_compiled_select();
$this->db->_reset_select();
// Main query
$this->db->select('*');
$this->db->where('property', $property);
$this->db->from('task t');
$this->db->join('($subquery) mt','mt.group = t.group');
$this->db->order_by('coalesce(mt.groupcomplete, t.complete)');
$this->db->order_by('coalesce(mt.mindate, t.due)');
$this->db->order_by('t.group');
$this->db->order_by('t.complete');
$this->db->order_by('t.due');
$query = $this -> db -> get();
// Return
return $query->result();
残念ながら、これはエラーをスローしているだけです:
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 'mt ON `mt`.`group` = `t`.`group` WHERE `property` = '7' ORDER BY coalesce(mt.gr' at line 3
CI によって報告されたクエリは次のようになります。
SELECT * FROM (`task` t) JOIN ($subquery) mt ON `mt`.`group` = `t`.`group` WHERE `property` = '7' ORDER BY coalesce(mt.groupcomplete, `t`.`complete)`, coalesce(mt.mindate, `t`.`date)`, `t`.`due`, `t`.`complete`, `t`.`date`
これを正しくフォーマットする方法についてアドバイスをくれる人はいますか? 残念ながら、私の mysql のスキルはほとんどないので、これが私の能力を押し上げています。この方法で (サブクエリを使用して) クエリを組み合わせた経験がないため、翻訳のアプローチの多くは Stack Overflow の回答からのものです。