[英]How to update column in one table from another if both tables have a text field in common
[英]How to update table with another table but no common field
我需要更新TableB中的值,但是公共字段中的數據並不完全相同。 這是我到目前為止所嘗試的
UPDATE TableB (`value`) SET (value * rate)
| TableA |
Rate t
2 2001
3 2002
4 2003
| TableB |
value t
1 2001-1
2 2001-2
3 2001-3
1 2002-1
2 2002-2
3 2002-3
1 2003-1
2 2003-2
3 2003-3
結果(抱歉,我沒有仔細檢查我的數據)
| TableB |
value t
2 2001-1
4 2001-2
6 2001-3
3 2002-1
6 2002-2
9 2002-3
4 2003-1
8 2003-2
12 2003-3
你可以嘗試這個
TRIM(SUBSTRING_INDEX(TableB.t,'-',1))
您的查詢
UPDATE TableB JOIN TableA ON TableA.t =
TRIM(SUBSTRING_INDEX(TableB.t,'-',1))SET TableB.value= TableB.value * TableA.rate
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.