简体   繁体   中英

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

And I want to display a list of links:

Similar properties in

L1 (4) L2 (5) L23 (27) and so on

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 ?

Use group by, something like

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 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.

You can do this all with your SQL query, using three functions:

SUBSTRING

COUNT

GROUP BY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM