gpt4 book ai didi

php - 一页显示多个 COUNT(*)

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:44 25 4
gpt4 key购买 nike

这可能非常简单 - 但我正在撕毁我的头发。数据库正在使用 cPanel (MariaDB 10.1.28)

场景是这样的

  • 1 个名为“警告”的表
  • 1 列名为“WARNING_REASON”(VARCHAR)warning_reason 包含各种“原因”

  • 1 列名为“WARNING_LEVEL”(VARCHAR)这包含“VERBAL”、“LEVEL 1”或“LEVEL 2”

我需要在 5 列表的 php 中输出此数据

  • 警告原因
  • 表中按 warning_reason 分组的总行数
  • 按 warning_reason 分组的匹配“VERBAL”的行总数
  • 按 warning_reason 分组的匹配“LEVEL 1”的行总数
  • 按 warning_reason 分组的匹配“LEVEL 2”的行总数

最佳答案

您可以使用子查询来完成此操作:

SELECT t1.warning_reason
, (SELECT COUNT(*) FROM warnings s1 WHERE s1.warning_reason = t1.warning_reason) AS total_count
, (SELECT COUNT(*) FROM warnings s2 WHERE s2.warning_reason = t1.warning_reason AND s2.warning_level = \'VERBAL\') AS verbal_count
, (SELECT COUNT(*) FROM warnings s3 WHERE s3.warning_reason = t1.warning_reason AND s3.warning_level = \'LEVEL 1\') AS level1_count
, (SELECT COUNT(*) FROM warnings s4 WHERE s4.warning_reason = t1.warning_reason AND s4.warning_level = \'LEVEL 2\') AS level2_count
FROM warnings t1
GROUP BY t1.warning_reason

关于php - 一页显示多个 COUNT(*),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46872749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com