-1

I have a table of zip codes in my MySQL database. It uses PHPMyAdmin and I'm using PHP code.

The table looks something like this:

zip_codes(id, zip, state)

The problem here is that I want to generate a quick list of the states in this table. However, I don't want a list of 33,000+ states that are 99.9% duplicates(many, many zip codes in each state). I just want to show one Alabama for instance. One Tennessee.

Is it possible to get a recordset like this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
SherwoodPro
  • 105
  • 10
  • Your question is way too vague - what DBMS are you using? What does your query look like? – ElGavilan Oct 31 '13 at 04:36
  • 1
    use "group by state" at the end of query.eg select state from zip_codes group by state – Krishnanunni Jeevan Oct 31 '13 at 04:37
  • 1
    In that case the states must have been put in a different table and a "many to one" relationship should have been established between zip_codes table and states table! Things would have been way simpler! – Arjun Oct 31 '13 at 04:38
  • It is MySQL, PHPMyAdmin and PHP. Shafeeq answered my question below. The reason there aren't two separate tables is because the zip code list I purchased had everything in one table. It is also necessary for each zip code to have its state. – SherwoodPro Oct 31 '13 at 04:45
  • use distinct for the state column – Chandan Kumar Oct 31 '13 at 04:46

1 Answers1

0

Use distinct

SELECT DISTINCT state FROM zip_codes 
Shafeeque
  • 2,039
  • 2
  • 13
  • 28
  • Thank you. That's easy to remember. I appreciate it. : ) – SherwoodPro Oct 31 '13 at 04:43
  • GROUP BY in many cases is way more efficient than doing a SELECT DISTINCT. Of course, it depends on the data and the desired result. I would try profiling both ways just to be sure. – ElGavilan Oct 31 '13 at 04:52