If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > SQL - Merging fields from two tables into one, including duplicates

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-04-08, 03:29
srinath.a srinath.a is offline
Registered User
 
Join Date: Sep 2008
Posts: 11
Question 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
Reply With Quote
  #2 (permalink)  
Old 09-04-08, 05:22
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
INSERT
  INTO C
     ( fooditem_id, taken )
SELECT fooditem_id, taken
  FROM nutritions
UNION ALL
SELECT fooditem_id, taken
  FROM food_options
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-04-08, 05:45
srinath.a srinath.a is offline
Registered User
 
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.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On