获得不同数量的成员

时间:2015-07-07 11:30:15

标签: mysql sql

也许我在这里很蠢但是考虑一下这个表

enter image description here

我想知道有多少唯一成员已注册,因此我使用此查询

$sql="SELECT DISTINCT member_nr, count(member_nr) as nr FROM Multiple_Orders 
                Where order = '$pTour' AND  round ='$pRound'";

我的问题

结果返回6

当然列中有6个成员,但 我想获得应该为2 的唯一成员数量,这就是我使用{{1}的原因}

我在这里缺少什么

2 个答案:

答案 0 :(得分:4)

这是您使用DISTINCT

的方式
$sql="SELECT DISTINCT(member_nr) as nr FROM Multiple_Orders 
                Where order = '$pTour' AND  round ='$pRound'";

将为您提供唯一成员列表

这就是你如何计算你有多少独特成员

$sql="SELECT COUNT(DISTINCT(member_nr)) as nr FROM Multiple_Orders 
                Where order = '$pTour' AND  round ='$pRound'";

将为您提供唯一成员的数量

答案 1 :(得分:2)

使用查询来获取不同成员的数量:

 $sql="SELECT count(distinct(member_nr)) as nr FROM Multiple_Orders 
                    Where order = '$pTour' AND  round ='$pRound' group by member_nr";

使用查询获取不同的成员:

 $sql="SELECT distinct(member_nr) as nr FROM Multiple_Orders 
                    Where order = '$pTour' AND  round ='$pRound' group by member_nr";

添加 group by 子句以获取每个成员的唯一成员和计数:

$sql="SELECT member_nr, count(member_nr) as nr FROM Multiple_Orders 
                Where order = '$pTour' AND  round ='$pRound' group by member_nr";