join using two column in sql
ProjectMaster
ProjectID ITManagerID DevelopmentManagerID
--------------------------------------------------
1 1000 1001
UserTable
UserID UserName
----------------
1000 Sam
1001 Ram
Result
Project ItManagerName DevManagerName
------------------------------------------------
1 sam ram
Help to write query
Edit: I tried
select
projectid,projectName,projectdescription,startdate,enddate,
apsmanagerid,projectmanager,ragstatus,projectstatus,projectpriority,
categoryid,inactivedate,comments,it.userName AS ITProjectManagerName,
dev.userName as DevManagerName
from pmis_project p,pmis_user It,pmis_user dev
where p.Devpro开发者_开发问答jectManager = It.userid
and p.ITmanagerid = dev.userid
and p.projectid IN (Select Projectid from SelectedProject)
You can JOIN
a table as many times as needed.
In this case
- one
JOIN
toUsers
to get the ITManager's name. - one
JOIN
toUsers
to get the DevManager's name.
SQL Statement
SELECT ProjectID
, ITManagerName = m.UserID
, DevManagerName = d.UserID
FROM ProjectMaster pm
INNER JOIN UserTable m ON m.UserID = pm.ITManagerID
INNER JOIN UserTable d ON d.UserID = pm.DevelopmentManagerID
You can include the same table multiple times in the FROM clause of a SELECT query. If you're doing this, you frequently want to introduce an alias for one or more of these tables, so that they can be referred to in the rest of the query. You introduce an alias by adding AS alias
after the name of the table, e.g.:
SELECT
*
FROM
Table As t
WHERE
t.Column = 'x' --<-- using the alias here
(the AS is actually optional)
Joining tables in a FROM clause is performed by using the JOIN
keyword, and placing the conditions for the join in the ON
:
FROM
Table1 t1
inner join
Table2 t2
on
t1.ColumnA = t2.ColumnB
SELECT * FROM ProjectMaster PM INNER JOIN UserTable UT ON UT.UserId = ITManagerId INNER JOIN UserTable UT1 ON UT1.UserId = DevelopmentManagerId
精彩评论