是否可以通过连接另一个表中的列数据来更新一个表列数据?让我用简单的例子来解释,
表A:studentaccess
╔════════╦══════════╗
║ UserID ║ AccCode ║
╠════════╬══════════╣
║ 12 ║ Tom ║
║ 13 ║ Ann ║
╚════════╩══════════╝
表B:studentdetails
╔════════╦═══════════════════╗
║ ID ║ UserName ║
╠════════╬═══════════════════╣
║ 1 ║ raj_12_kumar ║
║ 2 ║ test_13_test ║
╚════════╩═══════════════════╝
现在我要拆分UserName
列数据并从中查取第二个令牌,并通过匹配studentaccess
查询UserID
表并从中获取AccCode
值并将其与UserName
列数据的第二个标记连接起来。最后,我需要studentdetails
表数据,如下所示,
╔════════╦═══════════════════╗
║ ID ║ UserName ║
╠════════╬═══════════════════╣
║ 1 ║ 12_Tom ║
║ 2 ║ 13_Ann ║
╚════════╩═══════════════════╝
从下面的substr查询中,我可以从UserId
表中的UserName
列获取studentdetails
值,
select regexp_substr(UserName, '([^_]+)(_[^_]+){1}$', 1, 1, null, 1) as userId from studentdetails
任何人都可以帮助我与Acccode
连接相同内容并在UserName
列中更新相同内容吗?
答案 0 :(得分:2)
UPDATE StudentDetails sd
SET UserName =
(
SELECT userid||'_'||AccCode
FROM StudentAccess sa
WHERE regexp_substr(sd.UserName, '([^_]+)(_[^_]+){1}$', 1, 1, null, 1) = sa.userid
);
答案 1 :(得分:1)
您可以使用以下查询,我已经在我的测试和验证。
您可以在此sqlfiddle查看Oracle版本:http://sqlfiddle.com/#!4/cd66c/1
Oracle版
UPDATE studentdetails sd
SET sd.UserName =
(SELECT to_char(x.UserID) || '_' || sa.AccCode
FROM
(SELECT sd1.ID as ID, SUBSTR(sd1.UserName, INSTR( sd1.UserName,'_') + 1, INSTR( sd1.UserName,'_', INSTR( sd1.UserName,'_') + 1) - 1 - INSTR( sd1.UserName,'_')) AS UserID
FROM studentdetails sd1
) x INNER JOIN studentaccess sa ON TO_NUMBER(x.UserID) = sa.UserID where sd.ID = x.ID
);
SQL Server版本
UPDATE studentdetails
SET UserName =
(SELECT x.UserID + '_' + sa.AccCode
FROM
(SELECT SUBSTRING(sd1.UserName, CHARINDEX('_', sd1.UserName) + 1, CHARINDEX('_', sd1.UserName, CHARINDEX('_', sd1.UserName) + 1) - 1 - CHARINDEX('_', sd1.UserName)) AS UserID
FROM studentdetails sd1
WHERE sd1.ID = studentdetails.ID) x
INNER JOIN studentaccess sa ON x.UserID = sa.UserID);
更新1
如果studentaccess表中不包含从studentdetails表中获取的UserID,则可以在studentdetails表的UserName列中使用字符串UNKNOWN来使用下面的查询。
我无法在sqlfiddle中尝试,因为由于某些问题,sqlfiddle没有加载。无论什么时候,它都会重新上线,我会尝试一下让你知道。在此期间,您可以尝试这一点,让我知道任何问题。
UPDATE studentdetails sd
SET sd.UserName =
(SELECT CASE when x.ID is null then 'UNKNOWN' else to_char(x.UserID) || '_' || sa.AccCode END
FROM
(SELECT sd1.ID as ID, SUBSTR(sd1.UserName, INSTR( sd1.UserName,'_') + 1, INSTR( sd1.UserName,'_', INSTR( sd1.UserName,'_') + 1) - 1 - INSTR( sd1.UserName,'_')) AS UserID
FROM studentdetails sd1
) x RRIGHT OUTER JOIN studentaccess sa ON TO_NUMBER(x.UserID) = sa.UserID where sd.ID = x.ID
);
答案 2 :(得分:0)
你差不多了。您可以使用userName连接其他列。
regexp_substr(UserName, '([^_]+)(_[^_]+){1}$', 1, 1, null, 1) || '_' || acccode
您的样本数据的结果将是这样的 -
select userid, regexp_substr(UserName, '([^_]+)(_[^_]+){1}$', 1, 1, null, 1) || '_' || acccode as username from
(
select 1 UserID, 'Tom' AccCode from dual
union
select 1 userid , 'Ann' AccCode from dual) studentaccess,
( select 1 ID, 'raj_12_kumar' username from dual
union
select 2 id, 'test_13_test' username from dual) studentdetails
where studentaccess.userid = studentdetails.ID