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 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来