第一张图片是我的MySQL查询结果。 我想得到第二张图片。 请帮忙。
这是我的MySQL查询:
SELECT * FROM
(
SELECT
PPUser.[UserID] AS'UserID',
PPUser.[UserName] AS 'Name',
NULL AS 'RO',
NULL AS 'RO_UserID'
FROM [RF_ROTeacher]
LEFT JOIN PPUser ON RF_ROTeacher.UserID = PPUser.PUID
UNION ALL
SELECT
NULL AS 'UserID',
NULL AS 'Name',
PPUser.[UserID] AS 'RO',
PPUser.[UserName] AS 'RO_UserID'
FROM [RF_ROTeacher]
LEFT JOIN PPUser ON RF_ROTeacher.ROID = PPUser.PUID
)A
谢谢大家。
答案 0 :(得分:1)
您不需要两个不同的查询。试试这个
SELECT p.[UserID] AS'UserID',
p.[UserName] AS'Name',
f.[UserID] AS 'RO',
f.[UserName] AS 'RO_UserID'
FROM [RF_ROTeacher]
LEFT JOIN PPUser f
ON RF_ROTeacher.ROID = f.PUID
LEFT JOIN PPUser p
ON RF_ROTeacher.UserID = p.PUID
答案 1 :(得分:0)
您也可以尝试此查询以获得所需的结果:
SELECT UserID,Name,RO,RO_UserID FROM
(SELECT
ROW_NUMBER() OVER (PARTITION BY PPUser) AS row1,
PPUser.[UserID] AS 'UserID',
PPUser.[UserName] AS 'Name'
FROM [RF_ROTeacher]) A
LEFT JOIN
(SELECT
ROW_NUMBER() OVER (PARTITION BY PPUser) AS row2,
PPUser.[UserID] AS 'RO',
PPUser.[UserName] AS 'RO_UserID'
FROM [RF_ROTeacher])B
ON A.row1=B.row2