Results 1 to 2 of 2
  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
    Sep 2002
    Location
    UK
    Posts
    5,171
    Provided Answers: 1
    Like this?
    Code:
    create sequence c_id_seq;
    
    insert into c (id, fooditem_id, taken)
    select c_id_seq.nextval, fooditem_id, taken from nutritions
    union
    select c_id_seq.nextval, fooditem_id, taken from food_options;

Posting Permissions

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