dua55014 2014-05-23 08:55
浏览 119
已采纳

如何在CakePHP中编写带子查询的Select

I have the following query:

$queryResult = $this->Hit->query(
    "select P, count(S) as S
    from 
    (
        select pattern_id as P, srn as S from hits 
        where job_id=".$id." and srn != ''
        group by srn, pattern_id 
        order by pattern_id, srn
    ) as T 
        group by P
        order by P;"
);

So basically I have a select .. from (select .. ) ... statement. It's working perfect while I'm using MySQL. But I have to migrate the DB to PostgreSQL so I would like to change this to the Cake-way. So my question is, how can I interpret this type of query (select from select) in CakePHP?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dpqy77560 2014-05-23 11:24
    关注

    I am not sure what you mean by interpret this query in CakePHP since your code is valid CakePHP.

    That being said, I would rewrite your query as:

    SELECT pattern_id as P, COUNT(DISTINCT srn) as S
    FROM hits
    WHERE job_id=".$id." and srn != ''
    GROUP BY pattern_id
    ORDER BY pattern_id;
    

    (I believe this is equivalent - you could load sqlfiddle.com with sample data for better testing)

    If that works and you have a Hits model, then you could re-write the query using the find method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型