MYSQL: Multiple values in column

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eMonk
    Confirmed User
    • Aug 2003
    • 2310

    #1

    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
    Confirmed User
    • Aug 2006
    • 9231

    #2
    Make three tables.

    cities (varchar255 city_name, int11 auto incrementing ID)
    models (with all your model info)
    models_cities (model int11, city int11)
    Mechanical Bunny Media
    Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

    Comment

    • k0nr4d
      Confirmed User
      • Aug 2006
      • 9231

      #3
      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"
      Mechanical Bunny Media
      Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

      Comment

      • eMonk
        Confirmed User
        • Aug 2003
        • 2310

        #4
        How would you have, for example, Monica displayed in Toronto, Chicago, New York and London under the models_cities table?
        Last edited by eMonk; 03-12-2011, 06:02 AM.

        Comment

        • woj
          <&(©¿©)&>
          • Jul 2002
          • 47882

          #5
          Originally posted by eMonk
          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?
          Custom Software Development, email: woj#at#wojfun#.#com to discuss details or skype: wojl2000 or gchat: wojfun or telegram: wojl2000
          Affiliate program tools: Hosted Galleries Manager Banner Manager Video Manager
          Wordpress Affiliate Plugin Pic/Movie of the Day Fansign Generator Zip Manager

          Comment

          • k0nr4d
            Confirmed User
            • Aug 2006
            • 9231

            #6
            Originally posted by eMonk
            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.
            Mechanical Bunny Media
            Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

            Comment

            • redwhiteandblue
              Bollocks
              • Jun 2007
              • 2793

              #7
              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
              Interserver unmanaged AMD Ryzen servers from $73.00

              Comment

              • eMonk
                Confirmed User
                • Aug 2003
                • 2310

                #8
                Originally posted by k0nr4d
                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!
                Last edited by eMonk; 03-12-2011, 07:31 AM.

                Comment

                • KillerK
                  Confirmed User
                  • May 2008
                  • 3406

                  #9
                  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.

                  Comment

                  • mafia_man
                    Confirmed User
                    • Jul 2005
                    • 1965

                    #10
                    k0nr4d is right. Multi-valued attributes are bad.

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

                    Comment

                    • bbobby86
                      partners.sexier.com
                      • Jan 2007
                      • 11926

                      #11
                      Originally posted by redwhiteandblue
                      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...

                      Comment

                      • Davy
                        Confirmed User
                        • Apr 2006
                        • 4323

                        #12
                        Sounds like a typical N:M relationship. That is best modelled with another database table that connects the two other tables.
                        ---
                        ICQ 14-76-98 <-- I don't use this at all

                        Comment

                        • redwhiteandblue
                          Bollocks
                          • Jun 2007
                          • 2793

                          #13
                          Originally posted by Davy
                          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.
                          Interserver unmanaged AMD Ryzen servers from $73.00

                          Comment

                          • Tempest
                            Too lazy to set a custom title
                            • May 2004
                            • 10217

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

                            Comment

                            • borked
                              Totally Borked
                              • Feb 2005
                              • 6284

                              #15
                              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.

                              For coding work - hit me up on andy // borkedcoder // com
                              (consider figuring out the email as test #1)



                              All models are wrong, but some are useful. George E.P. Box. p202

                              Comment

                              • Tempest
                                Too lazy to set a custom title
                                • May 2004
                                • 10217

                                #16
                                Originally posted by borked
                                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.

                                Comment

                                Working...