简体   繁体   中英

Update a column based on two matching values of a column in two tables

I have two questions which if someone can help me it would be great and would be a great learning for me.

  1. I have two tables and my requirement is to update a column A of Table 1 value only for those rows for which the column B has values same as column B of Table2 .

    I am looking for an optimized query for this in SQL.

     UPDATE DBA.COM, DBA.MEN SET DBA.COM.ND_MAN='' WHERE DBA.MEN 

    After this, I couldn't select column names in where condition.

  2. The problem I am finding in Column B of both the tables is, it is unique identified (GUID) from the UI. So, when I copy the cell value from "SQL Anywhere" Interactive SQL Editor, it displays the column value copied as follows:

     0x99e2f2a23f9946acb0ceb374a627b142 

    and not as 99e2f2a23f9946acb0ceb374a627b142 .

    However, both the table's column value when I copy, it is starting with 0x . So will it not pose any problem I guess?

Or how to rectify it in above query which you will create for question 1?

You need to join and update something as

update table1 t1
join table2 t2 on t1.B = t2.B
set t1.A = 'some value'

Answer to your first question

UPDATE t1, t2 SET t1.name = new_value WHERE t1.id = t2.id;

Notes:

A multiple-table UPDATE is an extension of a single-table statement:

Following the UPDATE keyword, name the tables involved in the operation, separated by
commas. (You must name all the tables used in the query, even if you aren’t updating all
of them.)

In the WHERE clause, describe the conditions that determine how to match records in the
tables.

In the SET clause, assign values to the columns to be updated. These assignments can
refer to columns from any of the joined tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM