MySQL Sum Record Values

In this post I will show you an easy way to group and sum values in records. The most trivial example would be when you have a table of address records and you want to count how many people are from each state:

SQL:
  1. SELECT `state`, count( `state` ) AS num FROM `records` GROUP BY `state` ORDER BY `num` DESC

You could use a similar syntax to sum the distinct values in any field and count them.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Reddit
  • Furl
  • NewsVine
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • YahooMyWeb
  • TailRank

Home | MySQL | MySQL Sum Record Values