Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2008
    Posts
    11

    Question Unanswered: SQL - Merging fields from two tables into one, including duplicates

    Hi,

    I have two tables, nutritions and food_options.
    nutritions has two fields, fooditem_id, taken and .
    food_options has two fields, fooditem_id and taken.
    fooditem_id in nutritions and food_options will point to id in food_items table(third table).

    I want to select all records from nutritions and food_options into a new table, C with Fields fooditem_id and taken. This means I need to combine the values of fooditem_id in nutritions and fooditem_id in food_options into one field (fooditem_id), including the duplicates in both tables.

    EX:
    nutritions
    id fooditem_id taken
    1 23 1
    2 23 1

    food_options
    id fooditem_id taken
    1 34 1

    C
    id fooditem_id taken
    1 23 1
    2 23 1
    3 34 1

    How can I do this?

    thanks,
    srinath

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Code:
    INSERT
      INTO C
         ( fooditem_id, taken )
    SELECT fooditem_id, taken
      FROM nutritions
    UNION ALL
    SELECT fooditem_id, taken
      FROM food_options
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Sep 2008
    Posts
    11
    Hi r937,
    Thanks a lot for the Reply
    its working perfectly when used UNION ALL, i was trying using UNION and not getting from the last 3 days and finally you helped me this time

    thanks again for your Help

    Regards,
    Srinath.

Posting Permissions

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