[英]how to count unique records in mysql
I have a large property database and I am trying to get the postcode for a specific property then get the first letter ( the region ) and then count all other properties in my database in that region. 我有一个大型的属性数据库,我正在尝试获取特定属性的邮政编码,然后获取第一个字母(区域),然后计算该区域中我的数据库中的所有其他属性。
What I am after: 我所追求的是:
Property postcode is L21 5PU
物业邮政编码是
L21 5PU
And I want to display a list of links: 我想显示一个链接列表:
Similar properties in 类似的属性
L1 (4) L2 (5) L23 (27) and so on L1(4)L2(5)L23(27)等
Here is where I am upto 这是我最喜欢的地方
$PC = 'L';
PCSearch = mysql_query("SELECT * FROM Property WHERE POSTCODE LIKE '$PC'")
But how do I count each property whose postcode begins with $PC
( L in this case ) and display a list of the postcodes with the number next to them ? 但是,如何计算邮政编码以
$PC
开头的每个属性(在本例中为L)并显示邮政编码列表旁边的数字?
Use group by, something like 使用group by,类似于
SELECT postcode , count(*) as property_count FROM property GROUP BY postcode
That will group all the post codes and give you a property count. 这将对所有邮政编码进行分组,并为您提供属性计数。
If you want to do partial matching, I believe you will have to run a loop for each partial postcode you want looked up and add a "where" clause in the above query. 如果你想进行部分匹配,我相信你必须为你想查找的每个部分邮政编码运行一个循环,并在上面的查询中添加一个“where”子句。
.. WHERE postcode LIKE 'L%'
Try this query: 试试这个查询:
SELECT `postcode`, CONCAT(`postcode`, COUNT(`postcode`)) AS `output`
FROM `Property`
GROUP BY `postcode`
The column output
should now contain what you need. 列
output
现在应该包含您需要的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.