Unix Technical Forum

Add column from another table

This is a discussion on Add column from another table within the MySQL General forum forums, part of the MySQL category; --> I would like to add a column to a table where the column to be added is in another ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:34 AM
David Scott
 
Posts: n/a
Default Add column from another table


I would like to add a column to a table where the column to be added is in
another table. The column entries are to be matched using a particular
column from each table.

I have one table detailing phone calls, indexed by CallID. I have
created another table with the same index and a column CallNumber which I
want to add to the phone call table.

I have looked at documentation and in the book I have available without
success. Can anyone suggest appropriate syntax for this?

David Scot

__________________________________________________ _______________
David Scott Department of Statistics, Tamaki Campus
The University of Auckland, PB 92019
Auckland 1142, NEW ZEALAND
Phone: +64 9 373 7599 ext 86830 Fax: +64 9 373 7000
Email: d.scott@auckland.ac.nz

Graduate Officer, Department of Statistics
Director of Consulting, Department of Statistics

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:34 AM
Baron Schwartz
 
Posts: n/a
Default Re: Add column from another table

Hi David,

David Scott wrote:
>
> I would like to add a column to a table where the column to be added is
> in another table. The column entries are to be matched using a
> particular column from each table.
>
> I have one table detailing phone calls, indexed by CallID. I have
> created another table with the same index and a column CallNumber which
> I want to add to the phone call table.
>
> I have looked at documentation and in the book I have available without
> success. Can anyone suggest appropriate syntax for this?


Two queries come to mind. The first is the "official" SQL searched-update syntax:

update PhoneCalls set CallNumber = (
select CallNumber from OtherTable
where PhoneCalls.CallID=OtherTable.CallID);

The second is a multi-table update, which every RDBMS does differently:

update PhoneCalls as p
inner join OtherTable as o using(CallID)
set p.CallNumber=o.CallNumber;

Cheers
Baron
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:06 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com