Conditional query (sql)
-
Hi friends. I have 2 coulmns in a table (RealPersonID and CompanyID). They are foreign keys. My problem is that either of them is always 0 and the other one is none zero. So i want to do that in only one query: x=(Select CompanyID from Table1) y=(Select RealPersonID from Table1) if x!=0 (select companyName from Companies where companyID=x) else (select personName from Person where personID=y)
-
Hi friends. I have 2 coulmns in a table (RealPersonID and CompanyID). They are foreign keys. My problem is that either of them is always 0 and the other one is none zero. So i want to do that in only one query: x=(Select CompanyID from Table1) y=(Select RealPersonID from Table1) if x!=0 (select companyName from Companies where companyID=x) else (select personName from Person where personID=y)
Non tested!
Select IsNull(C.CompanyName, P.PersonName) Contact
From LinkTable L
Left Join ComapanyTable C on C.CompanyID = L.CompanyID
Left Join PersonTable P on P.RealPersonID = L.RealPersonIDThis may help you understand joins Visual Representation of SQL Joins[^]
Never underestimate the power of human stupidity RAH