Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 28th, 2008, 09:37 PM
Authorized User
 
Join Date: Oct 2006
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default update table column from another table

Hi,

I have the scenario below which I hope you could help me:

I have two tables:

TABLE_X:

xid xname
A
B
C

TABLE_Y:

yid | yname
A | A_NAME
B | B_NAME1
B | B_NAME2
C | C_NAME
C | C_NAME


I want to update the xname column witht he value of yname column, but all xid with different yname from table_y should have the value 'DUPLICATE'

TABLE_X:
xid | xname
A | A_NAME
B | DUPLICATE
C | C_NAME

As shown above, xid=B got xname=DUPLICATE because there are two yid=B in the table_Y with different yname. Thank you.

Last edited by elygp; December 28th, 2008 at 09:46 PM..
 
Old December 28th, 2008, 09:40 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I can't understand your tables because you didn't put [ code ] ... [ /code ] tags around them, so the fields all run together.

Also, I'd MUCH rather see the two tables COMPLETELY separate, not munged together.

And, finally: What database??? Yes, it matters.
 
Old December 28th, 2008, 09:44 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

The way I read your post, you have these existing tables:
Code:
TableX
xid :: xname
A   :: [null] 
B   :: [null] 
C   :: [null]

TableY
yid :: yname
A   :: A_NAME
B   :: B_Name
B   :: B_NAME2
C   :: C_NAME
Yes??? No???

And what you WANT from that is
Code:
TableX
xid :: xname
A   :: A_NAME
B   :: DUPLICATE
C   :: C_NAME
with TableY left unchanged.

Yes?? No???

If yes, we still need to know what DB, because multi-table UPDATEs are different in different DBs.

Also...what should be done if some xname *already* has a value???
 
Old December 28th, 2008, 10:02 PM
Authorized User
 
Join Date: Oct 2006
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Old Pedant View Post
The way I read your post, you have these existing tables:
Code:
TableX
xid :: xname
A   :: [null] 
B   :: [null] 
C   :: [null]
 
TableY
yid :: yname
A   :: A_NAME
B   :: B_Name
B   :: B_NAME2
C   :: C_NAME
Yes??? No???

And what you WANT from that is
Code:
TableX
xid :: xname
A   :: A_NAME
B   :: DUPLICATE
C   :: C_NAME
with TableY left unchanged.

Yes?? No???

If yes, we still need to know what DB, because multi-table UPDATEs are different in different DBs.

Also...what should be done if some xname *already* has a value???

Thanks Old Pedant for the reply. I forgot to mention, only those xname without a value (NULL or space) should be updated. Table Y will be left unchanged. Im using Sybase database. thank you so much!
 
Old December 28th, 2008, 11:21 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Ummm...you didn't answer either of my questions. Yes or No to both???

I *ASSUME* Yes to both, since you didn't object.

Unfortunately, I do *NOT* know the syntax of Sybase SQL. But since Microsoft's SQL Server is actually derived (more than 10 years ago) from Sybase, I will *GUESS* that it is the same. If not, you'll have to ask for some help in a Sybase forum, most likely.

Back later. Maybe tomorrow.
 
Old December 28th, 2008, 11:30 PM
Authorized User
 
Join Date: Oct 2006
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
Default

THanks anyway, but I think I got the code i need, I used "group by" clause to select the distinct xid, xname.

Last edited by elygp; December 29th, 2008 at 12:11 AM..
 
Old December 29th, 2008, 02:23 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Not sure how that's enough. Seems to me you need to do a check on the count, as in
Code:
    SELECT tableY.yid, MAX(tableY.yname) AS theNameToUse, COUNT(DISTINCT tableY.yname)
    FROM tableY
    GROUP BY tableY.yid
    HAVING COUNT(DISTINCT tableY.yname) = 1
or something along those lines.





Similar Threads
Thread Thread Starter Forum Replies Last Post
auto update from 1 table to column in another tabl headwaters Access 1 December 1st, 2008 08:39 AM
Update one table to another table using DTS in SQL Printmaker SQL Language 0 July 24th, 2007 07:17 AM
How can I update 2 rows in one column in SQL table babou Classic ASP Databases 6 March 9th, 2005 11:13 AM
update table with an ID Num from different table scoobie PHP How-To 12 January 25th, 2005 12:28 PM
Update parent table with the sum of child table gbrown SQL Language 2 November 9th, 2004 07:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.