Solved

SLQ Update

Posted on 2001-08-10
10
570 Views
Last Modified: 2007-11-27
Hi,
I have two tables and I want to update a field of want table based on the presence of the ID's in the other table.

For example:

Table A has the following records:

col1 col2
ID1  someData
ID2  someData
ID3  someData

Table B has the following records:

col1 col2
ID0  NULL
ID1  NULL
ID2  NULL
ID3  NULL
ID4  NULL

I want to update col2 in table B with 'Y' based on the occurence of the ID's in table A.  In this case, col2 of ID1,ID2, and ID3 in table B should be marked with 'Y' after the update.

How do I do this with a single SQL update statement?

Thanks.
Gamestors
0
Comment
Question by:gamestors
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6372656
Ths syntax may vary from the Database you are using:
MSSQL Server's Version:

UPDATE TableB
SET col2 = 'Y'
FROM TableB
JOIN TableA
ON TableA.col1 = TableB.col1

Cheers
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6372849
That's an sql server extension
I think this is ansi standard sql (could be wrong though).


update TableB
set col2 = 'Y'
where exists (select * from TableA where TableA.col1 = TableB.col1)

0
 

Author Comment

by:gamestors
ID: 6373178
I should have mentioned that I use MySql.
I tried both but neither one worked.

0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

Expert Comment

by:Elainec
ID: 6374640
gamestors
Try:

UPDATE tableB
set col2 = 'Y'
where col1 in (select col1 from tableA);

And yet another way may be:

UPDATE tableB INNER JOIN tableA
ON tableA.col1 = tableB.col1
SET tableB.col2 = 'Y';

A useful link for mySQL is www.mysql.com

Hope that helps & have fun!
Elaine
0
 

Expert Comment

by:dataliberator
ID: 6374649
not sure about mysql..... but you can use cursors I imagine?

DECLARE
--declaration of cursor, selects column1 values  from tablea, where ids match in tables
  CURSOR c1 IS
    SELECT b.column1
        FROM tablea a,
                    tableb b
    WHERE a.column1 = b.column1

-- creates a variable of same type of values selected in the cursor above (just 1 here, but could be many
--  columns)
   c1_rec c1%ROWTYPE;
 
BEGIN
-- this opend the cursor, and loops through it until there is no more data returned from the cursor
-- same as using OPEN c1; FETCH c1 INTO c1_rec, EXIT WHEN c1%NOTFOUND
-- but shorter obviously.......... - works in Oracle anyway
   FOR c1_rec IN c1 LOOP

       UPDATE TableB
               SET TableB. column2 = 'Y'
        WHERE TableB.column1 = c1_rec.column1;

       COMMIT;

  END LOOP

END;


-- Hope that helps..... and these lines are comments by the way.........
-- ~ Liberator
0
 

Author Comment

by:gamestors
ID: 6379871
Elainec,

According to the documentation on mysql.com, keywords 'IN', 'NOT IN',and 'NOT EXISTS' are not yet implemented in MySql.

Thanks anyway,

0
 

Accepted Solution

by:
Elainec earned 100 total points
ID: 6380069
Yes, but did you try the code:

UPDATE tableB INNER JOIN tableA
ON tableA.col1 = tableB.col1
SET tableB.col2 = 'Y';

??
I extracted this directly out of the mysql web site!
Elaine :-)
0
 

Author Comment

by:gamestors
ID: 6461670
I was looking for a simple way to do the update.  Your approach is too complicated. Thanks anyway.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6463088
i do not agree with the grade C you gave, gamestors.
i asked CS to correct this, because the approach isn't complicated at all, it is the way it works in SQL Server.
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20179769
Regards
0
 

Expert Comment

by:ComTech
ID: 6463243
I agree with the grade change.  We are currently changing our grading system, and any comment that leads to an answer deserves an A.

Thanks,
ComTech
Community Support
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question