需要使用联接查询 [英] Need Query using joins
问题描述
我有两个表,分别为Users和TimeEntry:
UserID用户名ReporteeID ID EmpCode
1公羊3 111
2 Shyam 3112
3卡兰4113
4亚历克斯4114
这是用户表,其中ReporteeId引用
的UserID
用户正在举报.例如Ram的"ReporteeId"为3,即
Karan用户ID,因此Ram正在向Karan报告.
现在这是TimeEntry表:
TimeEntryrID用户ID持续时间
123 1 12
124 2 14
125 4 12
126 6 17
我需要的结果是:-
用户名EmpCode ReporteeUserName
Ram 111 karan
Shyam 112 Karan
亚历克斯114 ALex
分别显示两个表中都存在的用户名,EmpCode和ReporteeUserName
即在用户和TimeEntry表上.
如果可能,请向我发送查询.
尝试此查询..
选择 A.UserName,A.EmpCode,B.UserName as ' ReporteeUserName' 来自用户 内部 join 用户上 A.ReporteeID = B.UserID 其中 A.UserID 在中(选择用户ID 来自 TimeEntry) pre>
希望这会有所帮助..
如果乐于助人,别忘了投票.
祝您编程愉快.
您可以使用INNER JOIN进行此操作.像这样的东西:
SELECT ... FROM 用户u1 INNER JOIN 用户u2 ON u2.UserId = u1.ReporteeId INNER JOIN TimeEntry te ON te.UserId = u2.UserId
这将带来定义ReporteeId的行以及具有时间条目的行,如果是一对多关系,则每个时间条目各1行.
您好,
这是 SQL联接
的最佳指南
经历一次.希望对您有帮助,
谢谢
-amit.
I have two tables as Users and TimeEntry:
UserID UserName ReporteeID EmpCode
1 Ram 3 111
2 Shyam 3 112
3 Karan 4 113
4 Alex 4 114
This is users table where ReporteeId refer the UserID to whom the
user is reporting.For example Ram''s "ReporteeId" is 3 i.e.
Karan UserID so Ram is Reporting to Karan.
Now this is TimeEntry table:
TimeEntryrID UserID Duration
123 1 12
124 2 14
125 4 12
126 6 17
I Need the Result as :-
UserName EmpCode ReporteeUserName
Ram 111 karan
Shyam 112 Karan
Alex 114 ALex
Dispaly the username,EmpCode and ReporteeUserName which exist on both table only
i.e on Users and TimeEntry Table.
Please Send me the query if possible.
try this query..
select A.UserName, A.EmpCode, B.UserName as 'ReporteeUserName' from Users as A inner join Users as B on A.ReporteeID=B.UserID where A.UserID in (Select UserID From TimeEntry)
hope this helps..
If Helpful dont forget to vote.
happy coding.
You can do this with INNER JOIN. Something like:
SELECT ... FROM Users u1 INNER JOIN Users u2 ON u2.UserId = u1.ReporteeId INNER JOIN TimeEntry te ON te.UserId = u2.UserId
That would bring the rows where the ReporteeId is defined and who have a time entry, 1 row per each time entry if it''s one-to-many relationship.
Hi,
Here is best tutorial for SQL Joining
go through it once. hope that will help you,
thanks
-amit.
这篇关于需要使用联接查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!