【问题标题】:MySQL query into Code Igniter Active Directory format?MySQL 查询成 Code Igniter Active Directory 格式?
【发布时间】:2013-11-12 14:03:12
【问题描述】:

我正在尝试让 mysql 查询进入 Code Igniter 的 Active Record 语法,但遇到了一些困难。

查询是这个问题的结果:Multiple mysql ORDER BY's for multidimensional ordering/grouping

我尝试自己格式化查询,解决了几个错误,但不确定如何进行。我必须自己将get_compiled_select() 函数添加到DB_active_rec.php,并将_reset_select() 从protected 更改为public 才能让它运行。

建议的查询是:

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 上的答案,因为我没有以这种方式组合查询的经验(与子查询)。

【问题讨论】:

  • 尝试使用$this->db->last_query() 查看您的查询有什么问题
  • 嗨 Drixson,该错误已在报告(如问题底部所述)。我确实坚持了该函数调用,但它对错误报告没有任何影响。我需要在更具体的地方称呼它吗?
  • 看到这个('task' t) 我认为它应该像('task') t 一样,尝试将t 替换为任务
  • 如果我的预感是正确的,我遇到了这个问题并且与$this->db->select('*')有关
  • @altsyset 供将来参考,反引号问题是 CI 中的一个已知错误 - github.com/EllisLab/CodeIgniter/issues/296 Whitespace 修复了它。

标签: php mysql codeigniter


【解决方案1】:

问题(或“问题之一”)在这里:

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

您使用单引号,因此变量 $subquery 不会被扩展。 这也可以在 CodeIgniter 输出的查询中看到。

【讨论】:

  • 编辑:$this->db->join($subquery.'mt','mt.group = t.group');似乎让子查询工作(我从这里得到了语法,这也一定是错误的stackoverflow.com/questions/14251358/…)。但是不幸的是,我仍然遇到语法错误。
  • 如果$subquery 中的查询已经包含在括号中,这可能会起作用。我不认为是,但您可以将其更改为$this->db->join("($subquery) mt",'mt.group = t.group');。唯一变化:双引号。如果你真的不喜欢它们,这样的连接也应该起作用:$this->db->join('('.$subquery.') mt','mt.group = t.group');
  • 啊,我明白了——这绝对有帮助!生成的查询现在看起来与您建议的查询非常相似 - 我看到的唯一问题是反引号没有正确包装合并 - 我将进行实验,看看我是否可以让输出看起来更好(你可以看到这个反引号我的问题中的问题)
  • 原来反引号问题是一个已知的 CI 错误 - 现在正在解决这个问题。
【解决方案2】:

当您有多个 order by 语句时,您可以像这样用逗号分隔它们

    $this->db->order_by('coalesce(mt.groupcomplete, t.complete), coalesce(mt.mindate, t.date), t.due, t.complete, t.date');

【讨论】:

  • 也一样,用多个$this->db->order_by();隔开
  • 正如 Drixson 指出的那样,部分语法并不是很相关(无论哪种方式都适用),我将它们分开纯粹是为了便于阅读。无论如何,结果都是一样的。
  • @DrixsonOseñathanx 的信息...我从来不知道
猜你喜欢
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
  • 1970-01-01
  • 2010-11-08
  • 2020-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多