dongwen5019 2013-11-12 14:03
浏览 41
已采纳

MySQL查询Code Igniter Active Directory格式?

I'm trying to get mysql query into Code Igniter's Active Record syntax but am having a bit of a hard time.

The query is as a result of this question: Multiple mysql ORDER BY's for multidimensional ordering/grouping

I've attempted to format the query myself, have tackled a couple of errors, but am unsure how to progress. I had to add in the get_compiled_select() function to DB_active_rec.php myself and change the _reset_select() from protected to public to get it to run at all.

The suggested query is:

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

My translation looks like this (note that there's a 'where' clause not in the original, and that 'date' is actually '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();

Unfortunately this is just throwing an error:

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

The query as reported by CI looks like:

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`

Anyone able to lend some advice as to how to get this formatted correctly? My mysql skills are, unfortunately, pretty bare, so this is pushing my abilities. Much of the approach of my translation is from answers on Stack Overflow, as I have no experience combining queries in this way (with the subquery).

  • 写回答

2条回答 默认 最新

  • dsymx68408 2013-11-12 14:34
    关注

    The problem (or 'one of the problems') is here:

    $this->db->join('($subquery) mt','mt.group = t.group');
    

    You use single quotes, so the variable $subquery doesn't get expanded. This can also be seen in the query that is outputted by CodeIgniter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line