gpt4 book ai didi

php - 使用 mysql 和 php 对多列进行计数

转载 作者:行者123 更新时间:2023-11-29 14:26:53 26 4
gpt4 key购买 nike

我有一个包含以下各列的销售表

  • 姓名
  • 类型(学生、成人、 worker )
  • 销售额(A、B、C、D)
  • 成本
  • 地点(1,2,3,4)

我想获取与名称和按名称分组相关的类型、销售和位置的计数,以便我可以将其显示在表格上 enter image description here

是否可以使用查询获取所有分组值>

任何帮助将不胜感激..

最佳答案

使用GROUP BY,然后使用SUM(condition)来获取计数:

SELECT   name AS Name,
SUM(type = 'worker' ) AS Worker,
SUM(type = 'student') AS Student,
SUM(type = 'adult' ) AS Adult,
SUM(sales = 'A' ) AS A,
SUM(sales = 'B' ) AS B,
SUM(sales = 'C' ) AS C,
SUM(sales = 'D' ) AS D,
SUM(location = 1 ) AS Location1,
SUM(location = 2 ) AS Location2,
SUM(location = 3 ) AS Location3,
SUM(location = 4 ) AS Location4
FROM sales
GROUP BY Name

关于php - 使用 mysql 和 php 对多列进行计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10784040/

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