【问题标题】:How to Dispaly Total Count Result in new Column如何在新列中显示总计数结果
【发布时间】:2016-07-27 11:14:44
【问题描述】:

我正在尝试从一个表和总状态计数计算 status,但它显示在查询所在的同一列中

select Status ,count(Status) from assigntask group by status union all
SELECT count(Status) , count(Status) as total from assigntask ;

和Reslut这样展示

Status    Count
Complete    1
Delay       2
  3         3

计数“3”是否可能不会显示在另一列中?

举例

Status   Count   Total
Complete   1       3
Delay      2

【问题讨论】:

  • “另一列”是什么意思。您还可以展示您的预期结果吗?
  • 添加预期结果的示例
  • 最后显示的总计数值我想在另一列中显示该值..我们可以在另一列中显示总计(3)

标签: mysql count


【解决方案1】:

试试这个:

select Status ,count(Status), null as totalCount from assigntask group by status union all
SELECT 'Total status' , null,  count(Status) from assigntask ;

这不是你想要的结果,但我认为这个结果比你理性展示的结果要好。

它会产生这个结果:

Status         Count     Total
Complete       1         null
Delay          2         null  
Total status   null      3

【讨论】:

  • 谢谢你对我来说也很好:)
  • 很高兴,我可以帮忙:)
【解决方案2】:

您必须在两个选择中提供一个虚拟字段,以便联合匹配。

SELECT Status, COUNT(Status) as count, NULL as total FROM assigntask GROUP BY status 
UNION ALL
SELECT NULL, COUNT(Status), COUNT(Status) FROM assigntask;

【讨论】:

    【解决方案3】:

    试试这个:

    select Status, count(Status) as cnt, t.total
    from assigntask
    cross join (
        SELECT count(Status) as total from assigntask
    ) t
    group by status
    

    【讨论】:

      【解决方案4】:

      如果你真的想在第一行得到总值,你可以使用这个查询:

      select t1.* from (
          select Status, count(Status) as count, (select count(Status) from assigntask) as total
          from assigntask 
          group by Status limit 1) as t1
      union all
      select t2.* from (
          select Status, count(Status) as count, null as total
          from assigntask 
          group by Status limit 1,100000) as t2
      

      结果是

      另一种方法是这样的:

      select Status, count(Status) as count, (select count(Status) from assigntask) as total
      from assigntask 
      group by Status
      

      给出以下结果

      我个人会使用两个单独的简单查询:

      select Status, count(Status) as count
      from assigntask 
      group by Status;
      
      select count(Status) as total from assigntask;
      

      【讨论】:

        猜你喜欢
        • 2015-02-01
        • 1970-01-01
        • 2012-01-15
        • 2018-03-03
        • 1970-01-01
        • 2017-05-04
        • 2012-05-12
        • 1970-01-01
        • 2017-09-08
        相关资源
        最近更新 更多