Search code examples
phpsqlinventory

Get results of sql query with identical rows grouped and their count returned as well


I have a table called "inventory" with 5 rows which are customer, manufacturer, type, destination and usable.

If I run something like "SELECT * FROM inventory where customer=..." then I can get all the entries for that customer and I can count the number of entries by counting the number of total results returned etc.

How do I get the results ordered and counted so that every entry for that customer with identical values are grouped and the total count for that group is returned.

Manufacturer,Type,Destination and Usable are all string values. The query should match all rows that have these four values identical and return these as groups with their individual group count

The values are very diverse but they will always be strings.

Is there any way to query the entire table in one go and return groups where the 4 rows have identical strings (Manufacturer,Type,Destination and Usable) and return one so we know what we have and the total count so we know how many for the entire table with SQL alone or does it need some PHP as well.

I am building this with Php and Sql


Solution

  • This ia a very simple count and group by

    Select customer, manufacturer, type, destination, usable, count(*) as Duplicates
    from inventory
    group by customer, manufacturer, type, destination, usable
    order by Duplicates;