繁体   English   中英

根据两个表中一列的两个匹配值更新一列

[英]Update a column based on two matching values of a column in two tables

我有两个问题,如果有人可以帮助我,那将是一件很棒的事情,对我来说将是一次很棒的学习。

  1. 我有两个表,我的要求是仅对B列的值与Table2 B列相同的行更新Table 1 A列值。

    我正在为此SQL寻找一个优化的查询。

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

    此后,我无法在where条件中选择列名称。

  2. 我在两个表的B列中发现的问题是,它是从UI唯一识别的(GUID)。 因此,当我从“ SQL Anywhere” Interactive SQL编辑器复制单元格值时,它将显示按以下方式复制的列值:

     0x99e2f2a23f9946acb0ceb374a627b142 

    而不是99e2f2a23f9946acb0ceb374a627b142

    但是,当我复制时,这两个表的列值都以0x 那我猜不会有什么问题吗?

或者如何在上面为问题1创建的查询中纠正它?

您需要加入并更新以下内容

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

回答你的第一个问题

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

笔记:

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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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