我的 postgresql 資料庫上有兩個表,如下所示。
[Table A] [Table B]
Name no Name no
------ ------ ------ ------
1. Adam 1 1. Candy null
2. Bob 2 2. Evan null
3. Candy 3 3. David null
4. David 4 4. Bob null
5. Evan 5 5. Adam null
[Table B Name] 有與 [Table A Name] 相關的外鍵。
[Table B no] 剛剛通過alter table 命令創建。
我這里要的是按照【表A編號】填寫【表B編號】一欄。
所以結果必須在下面。
[Table B]
no
------
3
5
4
2
1
有沒有什么好方法可以通過 SQL 實作這一點?
uj5u.com熱心網友回復:
UPDATE "Table B" AS b
SET no = a.no
FROM "Table A"
WHERE b.name = a.name
AND b.no IS NULL
uj5u.com熱心網友回復:
為什么不JOIN呢?
SELECT b.no
FROM [Table A] as a
INNER JOIN [Table B] as b
ON a.Name=b.Name
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/337120.html
標籤:sql PostgreSQL
下一篇:如何優化我們使用事務板的查詢?
