views:

182

answers:

2

Hai guys,

I have two tables Incharge and property. My property table has three fields 1stIncharge,2ndIncharge and 3rdIncharge. InchargeId is set as foreign key for all the above fields in the property table..

How to write a select statement that joins both the table.. I ve tried a bit but no result

select P.Id,P.Name,P.1stIncharge,P.2ndIncharge,P.3rdIncharge,I.Id from 
Property as P join Incharge as I where (\\How to give condition here \\)

Guys 3 fields P.1stIncharge, P.2ndIncharge, P.3rdIncharge has foreign key I.Id

Edit:

select P.Id,P.Name,P.1stIncharge,P.2ndIncharge,P.3rdIncharge,I1.Id from 
Property as P 
inner join Incharge as I1 on I1.Id=P.1stIncharge 
inner join Incharge as I2 on I2.Id=P.2ndIncharge  
inner join Incharge as I3 on I3.Id=P.3rdIncharge

and this query working
A: 

I'm not sure about your foreign key names from both tables, but I think you're looking for this:

SELECT P.Id, P.Name, P.1stIncharge, P.2ndIncharge, P.3rdIncharge, I.Id
FROM Property as P INNER JOIN Incharge as I ON P.InchargeId = I.Id
Codesleuth
@Codesleuth 3 fields P.1stIncharge, P.2ndIncharge, P.3rdIncharge has foreign key I.Id
Saranya
@Codesleuth there is no field called P.InchargeId
Saranya
Sorry, but your question was a bit vague. I'm glad you sorted it though.
Codesleuth
A: 

If your table and column names are correct, your provided solution looks ok (I reformatted it):

SELECT
  P.Id, P.Name, P.1stIncharge, P.2ndIncharge, P.3rdIncharge,
  I1.Id
FROM Property AS P
JOIN Incharge AS I1 ON ( I1.Id = P.1stIncharge )
JOIN Incharge AS I2 ON ( I2.Id = P.2ndIncharge )
JOIN Incharge AS I3 ON ( I3.Id = P.3rdIncharge )

What kind of error do you get?

Peter Lang