簡體   English   中英

在不使用鍵的情況下基於第二個表更新表

[英]Update table based on 2nd table without using the key

我有以下情況:

表格1

ID  DESC    TABLE2REF

1   1stRow  2
2   2ndRow  4
3   3rdRow  5
4   4thRow  3
5   5thRow  4
6   6thRow  5

表_2

ID  DESC 
1   Apples
2   Pears
3   Figs
4   Oranges
5   Grapes

不使用TABLE2REF的實際值(假定它是TABLE 2 ID的外鍵),我想創建一個更新查詢來更新當前引用Oranges或Grapes的TABLE 1行,以引用Figs。

我試過了:

我嘗試了各種查詢,但沒有成功。 任何幫助將不勝感激。 謝謝。

我不確定您的實際問題是什么。 如果要了解引用值,則需要table2ref (隱式或顯式)。

以下標識不使用table2OrangesGrapes的行:

update table1
    set table2ref = 3
    where table2ref in (4, 5);

您也可以選擇執行以下操作:

update table1
    set table2ref = 3
    where in (5, 6);

但是,正確的方法是:

update table1
    set table2ref = (select id from table2 where description = 'Figs')
    where table2ref in (select id from table2 where description in ('Oranges', 'Grapes'));

如果您想使用一般的方式,這可以為您提供幫助:

UPDATE table1 tab1 SET table2ref = tab2.id 
FROM table2 tab2 
WHERE tab2.id = tab1.table2ref

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM