我想通過連接兩個不同的表來執行更新操作。如何使用兩個表執行更新
My tables are :
dim_sess
name role
20111012133513aaa123
20110908072611aaa121
20111002210235bbb853
20120113113353bbbl971
角色列稍後更新。
,另一臺是
employeerole
username role thedate
aaa technician 2011-10-12 13:35:13
aaa technician 2011-09-08 07:26:11
bbb day guard 2011-10-02 21:02:35
bbb day guard 2012-01-13 11:33:53
bbb night guard 2012-01-13 21:30:00
我要更新這取決於用戶名和STARTDATE的dim_sess表的作用。同一個用戶可以在不同的日期有不同的角色。兩個表中沒有公共列,唯一要執行連接的列是dim_sess中的「name」。
我編寫了以下查詢來更新dim_ses表。
UPDATE dim_sess
SET role = (SELECT employeerole.role FROM employeerole
WHERE SUBSTRING(dim_sess.name,15,7) = employeerole.username
AND SUBSTRING(dim_sess.name,1,14) = (TEXTCAT(TEXTCAT(TEXTCAT(SUBSTRING(thedate,1,4), SUBSTRING(thedate,6,2)) , TEXTCAT(SUBSTRING(thedate,9,2), SUBSTRING(thedate,12,2))), TEXTCAT( SUBSTRING(thedate,15,2), SUBSTRING(thedate,18,2)))))
WHERE SUBSTRING(dim_sess.name,1,21) = (SELECT TEXTCAT((TEXTCAT(TEXTCAT(TEXTCAT(SUBSTRING(thedate,1,4), SUBSTRING(thedate,6,2)) , TEXTCAT(SUBSTRING(thedate,9,2), SUBSTRING(thedate,12,2))), TEXTCAT(SUBSTRING(thedate,15,2), SUBSTRING(thedate,18,2)))) , employeerole.username) AS session FROM employeerole);
The error message I get is:
ERROR: more than one row returned by a subquery used as an expression
既然有兩個表之間沒有共同的專欄中,我嘗試使用子功能相匹配的結果加入其中。但這似乎是一個不好的解決方案。我想知道是否有其他方式來加入這兩個表。
添加一列到dim_sess如姓名 – Matten