0

这就是我目前所拥有的。

    $db =& JFactory::getDBO();

    $query = $db->getQuery(true);

    $query->select('`#__catalog_commit`.`id` as id, `#__catalog_commit`.`date` as date, COUNT(`#__catalog_commit_message`.`commit_id`) as count,
    (SELECT COUNT(`#__catalog_commit_message`.`type`) as count_notice FROM `#__catalog_commit_message` WHERE `#__catalog_commit_message`.`type` = 1 GROUP BY `#__catalog_commit_message`.`type`) as count_notice,
    (SELECT COUNT(`#__catalog_commit_message`.`type`) as count_warning FROM `#__catalog_commit_message` WHERE `#__catalog_commit_message`.`type` = 2 GROUP BY `#__catalog_commit_message`.`type`) as count_warning,
    (SELECT COUNT(`#__catalog_commit_message`.`type`) as count_error FROM `#__catalog_commit_message` WHERE `#__catalog_commit_message`.`type` = 3 GROUP BY `#__catalog_commit_message`.`type`) as count_error');
    $query->from('#__catalog_commit_message');
    $query->leftjoin('`#__catalog_commit` ON `#__catalog_commit`.`id` = `#__catalog_commit_message`.`commit_id`');
    $query->group('`#__catalog_commit_message`.`commit_id`');
    $query->order('`#__catalog_commit`.`id` DESC');

我拥有的是 2 个具有以下结构的表:

catalog_commit
==============
id
date

catalog_commit_message
======================
id
commit_id
type
message

基本上我想计算每个组项目的每种不同类型的消息。在我实际选择的每一行中(这是正常的),但我正在寻找一种方法(如果可能的话,更好)在查询中对每个消息类型进行计数。

编辑:只是想补充一点,它是一个 JModelList。

4

1 回答 1

0

根据我收集的信息,这应该是您的查询:

SELECT c.id
      ,c.date
      ,count(cm.commit_id) as ct_total
      ,sum(CASE WHEN cm.type = 1 THEN 1 ELSE 0 END) AS count_notice
      ,sum(CASE WHEN cm.type = 2 THEN 1 ELSE 0 END) AS count_warning
      ,sum(CASE WHEN cm.type = 3 THEN 1 ELSE 0 END) AS count_error
FROM   catalog_commit c 
LEFT   JOIN catalog_commit_message cm ON cm.commit_id = c.id
GROUP  BY c.id, c.date
ORDER  BY c.id DESC

您在 中颠倒了表格的顺序LEFT JOIN。此外,您在 SELECT 列表中有奇怪的子查询。

于 2011-11-28T21:35:03.277 回答