从数据库打印多个结果

时间:2012-03-14 13:29:29

标签: php sql database

目前我有一个sql查询,它查找数据库中特定人员(White White)的记录数量,如何为表格中的每个姓氏重复该功能并以合理的格式打印出来?

  $query = "SELECT COUNT(Surname) FROM Customers WHERE Surname='White'";

3 个答案:

答案 0 :(得分:5)

GROUP BY我认为你正在寻找

SELECT COUNT(*),Surname FROM Customers GROUP BY Surname

答案 1 :(得分:3)

每个姓氏只运行count一个查询会不会更好?

select Surname, count(*) as Total from Customers
group by Surname

这将返回如下结果:

Person  Total
White       4
Mustard     2
Plum        1
etc...

答案 2 :(得分:0)

使用

SELECT count(1) as "Counter", surname FROM Customers GROUP BY surname

这将为您提供如下输出。

Counter  +  Surname
++++++++++++++++++++
  10     +  SN1
  15     +  SN2
  11     +  SN3