0

How do i find how many items there are per unique user in a data base

So say i have hats gloves and lots of other elements in my data base that users have added

how would i find how much each one had

e.g. hats- 35 t-shirts -50

but the problem is the categories would change so it would have to get all the categories then do an MySQL search on each of them... i have done lots of different MySQL tries and they have all failed here is one

$sql = "SELECT COUNT(username) FROM cards GROUP BY username";
$query = mysqli_query($db_conx, $sql);
$row = mysqli_fetch_row($query);
$users = $row[0];

and i dont know where to go from here

  • Can you post more SQL-Information? How are you Tables structured, how can you select all tshirts or hats? or what is a card etc? – Visionstar Feb 09 '14 at 16:58

1 Answers1

0

This is how you can do

select `item`,count(*) as `total` from `test` 
group by `item`

check here

http://www.sqlfiddle.com/#!2/19250/1

You can change the query to have your field names in the query

Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63