GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   MYSQL: Multiple values in column (https://gfy.com/showthread.php?t=1013875)

eMonk 03-12-2011 07:37 AM

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?

k0nr4d 03-12-2011 07:41 AM

Make three tables.

cities (varchar255 city_name, int11 auto incrementing ID)
models (with all your model info)
models_cities (model int11, city int11)

k0nr4d 03-12-2011 07:43 AM

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"

eMonk 03-12-2011 07:55 AM

How would you have, for example, Monica displayed in Toronto, Chicago, New York and London under the models_cities table?

woj 03-12-2011 08:07 AM

Quote:

Originally Posted by eMonk (Post 17974637)
How would you have, for example, Monica displayed in Toronto, Chicago, New York and London under the models_cities table?

add it 4 times into that table?

k0nr4d 03-12-2011 08:09 AM

Quote:

Originally Posted by eMonk (Post 17974637)
How would you have, for example, Monica displayed in Toronto, Chicago, New York and London under the models_cities table?

You would insert multiple models_cities entries per model.

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.

redwhiteandblue 03-12-2011 08:31 AM

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

eMonk 03-12-2011 09:30 AM

Quote:

Originally Posted by k0nr4d (Post 17974653)
You would insert multiple models_cities entries per model.

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.

Ah thanks bro. I'll try this tonight or tomorrow. I was thinking along the lines of:

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!

KillerK 03-12-2011 11:41 AM

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.

mafia_man 03-12-2011 12:24 PM

k0nr4d is right. Multi-valued attributes are bad.

You need to normalise your data. It will help performance when these tables get huge.

bbobby86 03-12-2011 12:48 PM

Quote:

Originally Posted by redwhiteandblue (Post 17974692)
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

also can try inner join...

Davy 03-12-2011 01:55 PM

Sounds like a typical N:M relationship. That is best modelled with another database table that connects the two other tables.

redwhiteandblue 03-12-2011 02:01 PM

Quote:

Originally Posted by Davy (Post 17975170)
Sounds like a typical N:M relationship. That is best modelled with another database table that connects the two other tables.

Yes like the table "model_in_city" that he describes in his OP, which you would query with the SQL I wrote, although I would have called the table something like "model_city_rel" to make it obvious what it's for.

Tempest 03-12-2011 02:30 PM

What k0nr4d said. And make sure you put an index on model_in_city like KEY idx1 (city_id,model_id)

borked 03-12-2011 03:35 PM

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.

Tempest 03-12-2011 05:07 PM

Quote:

Originally Posted by borked (Post 17975289)
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

That's interesting as I've read enough about table naming conventions and design to know that most of the more hardcore database designers will use singular names.. i.e. model, city and not models, cities... Mostly since the tables are a collection of singular elements and it's the elements that the table contains that you're interested in.

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