sql语句实现问题,一个表既有创建者和更新者,只有一个用户表,怎么同时把更新者和创建者查出来
表test
id test creatorID updateID
1 test 001 002
2 test1 002 001
用户表 user
userid username
001 小王
002 小李
用最简单的方法实现把表A中的创建者和更新者一次查出。
eg:
SELECT
C.TEST ,
C.USERNAME ,
D.USERNAME
FROM
(
SELECT
A.TEST ,
B.USERNAME ,
A.UPDATEID
FROM
TEST A LEFT JOIN
USER B
ON A.CREATORID =B.USERID ) C LEFT JOIN
USER D
ON C.UPDATEID=D.USERID
------解决方案-------------------- SELECT
A.TEST ,
B.USERNAME ,
C.USERNAME
FROM
TEST A
LEFT JOIN USER B
ON A.CREATORID =B.USERID
LEFT JOIN USERC
ON A.updateID=C.USERID
------解决方案--------------------
SQL code
select test,creator=s.USERNAME,updator=t.USERNAME
from test join user s on test.creatorID=s.userid
join user t on test.updateID=t.userid
------解决方案--------------------
SELECT
A.TEST ,
B.USERNAME as create_name,
C.USERNAME as update_name
FROM
TEST A
LEFT JOIN USER B
ON A.CREATORID =B.USERID
LEFT JOIN USER C
ON A.updateID=C.USERID
上面 那个有错误,表B 说明是创建者,表C说明是更新者。