![]() |
MYSQL: Multiple values in column
My database and columns:
province (province_id, ..) city (city_id, province_id, ..) model (model_id, age, height_in_cm, hair_color, measurements, eye_color, description, ..) model_in_city (model_id, city_id) Now I want to display all models in the chosen city by the user but each model can chose up to 4 city placements. How can this be done? |
Make three tables.
cities (varchar255 city_name, int11 auto incrementing ID) models (with all your model info) models_cities (model int11, city int11) |
Ahh you were listing tables not columns.
SELECT model.* FROM model WHERE model.model_id = model_in_city.model_id AND model_in_city.city_id = 12345 OR SELECT model.* FROM model WHERE model.model_id = model_in_city.model_id AND model_in_city.city_id = city.city_id AND city.city_name = "Toronto" |
How would you have, for example, Monica displayed in Toronto, Chicago, New York and London under the models_cities table?
|
Quote:
|
Quote:
So lets say Toronto = 123 Chicago = 456 New York = 789 INSERT INTO models_cities (model, city) VALUES (modelid,123); INSERT INTO models_cities (model, city) VALUES (modelid,456); INSERT INTO models_cities (model, city) VALUES (modelid,789); SELECT model.* FROM model WHERE model.model_id = model_in_city.model_id AND model_in_city.city_id = city.city_id AND city.city_name = "Toronto" SELECT model.* FROM model WHERE model.model_id = model_in_city.model_id AND model_in_city.city_id = city.city_id AND city.city_name = "Chicago" SELECT model.* FROM model WHERE model.model_id = model_in_city.model_id AND model_in_city.city_id = city.city_id AND city.city_name = "New York" Will then all return the same model Edit: well, insert using your actual table and column names i just wrote those inserts quickly to show you. |
select * from model, city
left join model_in_city on model.model_id=model_in_city.model_id left join city on city.city_id=model_in_city.city_id |
Quote:
VALUES (modelid, 123, 456, 789, ...); Which you can't do from my understanding (multiple values in column). I searched google and my book for reference but couldn't find anything on this. Thanks again! |
You could have one column called like cities_in or something
and then store it as 14|29|30 then just explode the value from that, however that's not the right way to do it. You need to use multiple tables. |
k0nr4d is right. Multi-valued attributes are bad.
You need to normalise your data. It will help performance when these tables get huge. |
Quote:
|
Sounds like a typical N:M relationship. That is best modelled with another database table that connects the two other tables.
|
Quote:
|
What k0nr4d said. And make sure you put an index on model_in_city like KEY idx1 (city_id,model_id)
|
This is why plural/singular naming of tables is important, but often overlooked. Not important as in "shit won't work" but important in seeing at a glance relationships....
models cities model_city says automatically that the models tables is where all the models are, likewise for the cities, but modelcity is a singular relationship between a model and city. minor detail, but still important... btw, make those tables innodb and add their relationships, so that if you ever delete a model, all her/his entries in model_city disappear automatically also... one of the great benefits of relationships. |
Quote:
Just a small example of what I'm getting at, SELECT model.name FROM model is more "readable" than SELECT models.name FROM models.. There are also more readability issues for different languages. At the end of the day though it comes down to your personal preferences, what engine, language etc. you're using to develop in. The most important thing is to be consistent. |
All times are GMT -7. The time now is 01:25 AM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123