【问题标题】:SQL Query - For Count of Records [duplicate]SQL 查询 - 记录计数 [重复]
【发布时间】:2020-03-28 10:45:57
【问题描述】:

我有一个名为“history”的表(列名是:ID、Records、Stage、Date),其中有以下记录:

1    Record1    Stage1  Date
2    Record2    Stage1  Date
3    Record3    Stage1  Date
4    Record1    Stage2  Date

所有具有一定优先级的记录都保存在名为“记录”的主表中(列名称为:id、记录、舞台名称、日期),如下所示

1   Record1 High    Date    
2   Record2 Low Date
3   Record3 Medium  Date
upto more than 100+ records

所以我想显示 High Low 和 Medium 的 COUNT 期望的输出:

[#] 第一阶段

High - 1
Low - 1
Medium -1 

[#] 第 2 阶段

High - 1
Low - 0
Medium - 0

你能帮我解决这个问题吗?

这是我的代码:

$stagename = "Stage 1";
$query= $conn->prepare("SELECT count(*) FROM history WHERE stagename=?");
$stmt->execute(array($getstagename));
$count= $stmt->fetchColumn();

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    我在猜测列名(因为您没有包含它们),但查询应该是这样的:

    select
      h.stage,
      r.priority,
      count(*)
    from history h
    join records r on r.record_name = h.record_name
    group by h.stage, r.priority
    order by h.stage, r.priority
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多