我想得到每个邮政编码的属性数

时间:2013-05-23 15:15:03

标签: php mysql database

我有一个包含property_idproperty_nameproperty_zip

的数据库

在页面上,我使用以下方式加载所有邮政编码:

<?php include"connection.php";

$checke = "SELECT DISTINCT porperty_zip FROM property_list "; 

$rsd = mysql_query($checke); ?> 

<table width="600" style="font-size:12px; border:1px solid #000000; text-align:center;" cellpadding="0" cellpadding="0">

<tr> 

<td><strong>Zip</strong></td> </tr> 

<?php 

while($row = mysql_fetch_array($rsd)) 

{ ?> 

<tr> <td><h2><?php echo $row['porperty_zip']; ?></h2></td> </tr> 

<?php 

} 

?>

输出:

  

92508

     

92566

现在任何人都可以帮我添加每个邮政编码的属性数。

我希望输出看起来像:

  

92508(15)

     

92566(20)

4 个答案:

答案 0 :(得分:3)

SELECT property_zip,count(*) as property_count FROM property_list GROUP BY property_zip

然后在html:

<tr> <td><h2><?php echo $row['property_zip'].'('.$row['property_count'].')'; ?></h2></td> </tr> 

答案 1 :(得分:3)

您需要GROUP BYCOUNT ...

SELECT COUNT(*) as property_count, property_zip FROM property_list GROUP BY property_zip

答案 2 :(得分:2)

使用此查询:

SELECT 
  porperty_zip ,
  COUNT(*) as nb
FROM property_list
GROUP BY
  porperty_zip

在php方面:

echo $row['porperty_zip']." ".$row['nb'];

答案 3 :(得分:2)

使用SQL,如: -

SELECT property_zip, COUNT(*) AS property_count FROM property_list GROUP BY property_zip

然后你可以使用

<tr> <td><h2><?php echo $row['property_zip']; ?>(<?php echo $row['<?php echo $row['property_count']; ?>']; ?>)</h2></td> </tr> 

编辑 - 获取每个注册用户的属性数量(假设该表是registered_user_details): -

SELECT a.user_id, a.name, a.zip, COUNT(b.property_zip) AS PropertiesForUser
FROM registered_user_details a
LEFT OUTER JOIN property_list b
ON a.zip = b.property_zip
GROUP BY a.user_id, a.name, a.zip