Reputation: 18948
I had a select box where the user will select the list of cities they travelled. After that the admin will search for the users with particular cities.
I am very confused with the structure of the database. There are totally 300 cities. Creating column for each city and store it with boolean on or off, looks a childish technique. Any one help me plz
Upvotes: 0
Views: 524
Reputation: 122624
Not sure why you've tagged this both mysql
and sql-server
- are you using both?
Anyway, this is a standard many-to-many mapping:
Table: User
Table: City
Table: UserCity
To retrieve all of the cities for a given user:
SELECT c.CityID, c.CityName
FROM User u
INNER JOIN UserCity uc
ON uc.UserID = u.UserID
INNER JOIN City c
ON c.CityID = uc.CityID
WHERE u.UserID = @UserID
Upvotes: 5
Reputation: 69362
You don't have to create a column for each city. You simply create a "City" column, where each row is a city. You then have a "Visited" column, where each row is a boolean. In total, there are only two columns.
City | Visited
London | 1
Paris | 0
New York | 1
etc...
Upvotes: 0