0

I have following table in my database: enter image description here

All I need is to load Country list for User with ID=5:

  • USA
  • Canada
  • Germany

Then, when I will click for example on USA, it will load the cities for that country:

  • Washington DC
  • Miami

I can display country field for every row where User ID=5 but I will have the following result which is not acceptable.

  • USA
  • Canada
  • USA
  • Germany
  • Canada
  • Germany

Are there any tricks which can help me not to duplicate country values?

EducateYourself
  • 971
  • 2
  • 13
  • 32
  • 2
    Perhaps something like this: `SELECT DISTINCT Country FROM WHERE UserID = 5;` http://www.mysqltutorial.org/mysql-distinct.aspx
    – Cyclonecode Aug 03 '14 at 05:43

2 Answers2

2

You should be able to use DISTINCT to remove duplicate values from your resultset:

SELECT DISTINCT Country FROM <table> WHERE UserID = 5;

Reference: How to Use MySQL DISTINCT to Eliminate Duplicate Rows

Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
1

As Krister says, part 1 is

SELECT DISTINCT COUNTRY FROM TABLE WHERE USERID =5

Then to get cities for that user in USA:

SELECT CITY FROM TABLE WHERE USERID =5 AND COUNTRY = 'USA'

Avner
  • 4,286
  • 2
  • 35
  • 42