TABLE dbo_R5PERSONNEL
column PER_DESC
Do, John
Jones, Jacky
TABLE dbo_R5USERS
column USR_DESC
John Do
Jack Jones
问:如何连接这两个表以从表 dbo_R5USERS 中获取表 dbo_R5PERSONNEL 的列 PER_DESC 中的所有 USR_DESC。
我使用字符串操作来大写和提取每个表中的姓氏
SELECT UCASE(MID(TRIM(dbo_R5USERS.USR_DESC), INSTR(TRIM(dbo_R5USERS.USR_DESC), ' ') + 1, LEN(TRIM(dbo_R5USERS.USR_DESC)) - INSTR(TRIM(dbo_R5USERS.USR_DESC), ' '))) AS LastName
FROM dbo_R5USERS
WHERE dbo_R5USERS.USR_ACTIVE = '+'
ORDER BY dbo_R5USERS.USR_DESC;
SELECT UCASE(MID(TRIM(dbo_R5PERSONNEL.PER_DESC),1,INSTR(TRIM(dbo_R5PERSONNEL.PER_DESC),',')-1)) AS LastName
FROM dbo_R5PERSONNEL
WHERE dbo_R5PERSONNEL.PER_NOTUSED='+'
ORDER BY dbo_R5PERSONNEL.PER_DESC;
我将如何在这两个子查询中加入 JOIN?还有其他方法可以达到我想要的结果吗?
提前致谢!
更新1:
SELECT R.LastName
, P.LastName FROM
(
SELECT UCASE(MID(TRIM(dbo_R5USERS.USR_DESC)
, INSTR(TRIM(dbo_R5USERS.USR_DESC), ' ') + 1, LEN(TRIM(dbo_R5USERS.USR_DESC)) - INSTR(TRIM(dbo_R5USERS.USR_DESC), ' '))) AS LastName
FROM dbo_R5USERS
WHERE dbo_R5USERS.USR_ACTIVE = '+'
) R
INNER JOIN
(
SELECT UCASE(MID(TRIM(dbo_R5PERSONNEL.PER_DESC),1,INSTR(TRIM(dbo_R5PERSONNEL.PER_DESC),',')-1)) AS LastName
FROM dbo_R5PERSONNEL
WHERE dbo_R5PERSONNEL.PER_NOTUSED='+'
) P
ON R.LastName = P.LastName