Results 1 to 3 of 3

Thread: [resolved] product table - colors table subquery questions.

  1. #1

    Thread Starter
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    Resolved [resolved] product table - colors table subquery questions.

    Thinking of how to go about this:

    I have a products table with many products.
    These products come in various colors.

    In my products table, I would like to have one field, [color], that indicates
    the various colors this product is available in.

    Also, I have a color table, with [colorID], [color] fields.


    In the [product].[color] field, I tried inserting data like:
    1,2,4,6 and using a subquery such as:
    select color from color where colorid in (select color from products where productid = 1)

    but with no luck.



    My ideal example:
    If a user selects a product, a drop down is populated with
    all of the available colors.


    Wondering how to go about creating the sql for retrieving
    this.

    Is there another way to do this?
    Last edited by ZeBula8; Dec 4th, 2006 at 10:36 PM. Reason: resolved

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: product table - colors table subquery questions.

    In my products table, I would like to have one field, [color], that indicates the various colors this product is available in.
    Never use a single field to indicate multiple values.

    Normalize your database by creating another table called ProductColors (ProductId, ColorId). This allows you to get the colors for a single product and also allows you to get all the products that come in a specific color.

    For your dropdown, the sql statement would simply be

    Code:
    Select PC.ProductId, PC.ColorId, C.Color 
    From ProductColors PC Inner Join Colors C On C.ColorId = PC.ColorId
    Where PC.ProductId = ?

  3. #3

    Thread Starter
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    Resolved Re: product table - colors table subquery questions.

    this is what I've been struggling for - very much appreciated brucevde !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width