如何把这个两条语句合起来
1.select CreateID,code,name,CreateDate from usertab where code=001
2.SELECT name as Createname FROM usertab where code=CreateID
usertab结构如下:
ID, Code, Name, CreateID, CreateDate
ID 自动编号
code 用户编号
name 用户名称
CreateID 记录创建者ID
CreateDate 记录创建时间
ID, Code, Name, CreateID, CreateDate
1,001,name1,002,2007-1-1
2,002,name2,003,2007-2-2
问题,我现在想查找出用户编号001的全部信息和记录创建者名称(注意是名称而不是创建者ID),用一条语句实现
------解决方案--------------------select [ID],
CreateID,
code,
name,
CreateDate,
CreateName = (SELECT name
FROM usertab AS b
WHERE b.[ID] = a.CrateID)
from usertab AS a
where code=001
------解决方案----------------------用子查询
select [ID],
CreateID,
code,
name,
CreateDate,
(SELECT name
FROM usertab AS b
WHERE b.[ID] = a.CrateID) as CreateName
from usertab AS a
where code=001
------解决方案-------------------- select CreateID,code,name,CreateDate,name as Createname from usertab where code=001 and code=CreateID group by CreateID,Createname;