Sumitha
08-12-2008, 02:30 AM
I am having some problem with a query. I have 3 tables - A, B and C. A is the main table. There is a 1-M relationship between A & B and a 1-1 relationship between A & C. I want to select all the fields from all the 3 tables so that I can create a Report. My SQL query goes somewhat like this:
SELECT A.CodeNo, A.District, A.Agency, A.Address, A.Nature, A.RegNo, A.RegPlace, A.RegDate, A.RegCert, A.MoA, A.Audit, A.Contact, A.Pin, A.Phone, A.PastExp, B.Year, B.Grant, B.UtilCert, B.Report, B.AccState, A.Awareness AS A_Awareness, A.Action AS A_Action, C.Awareness AS C_Awareness, C.Action AS C_Action, C.Total, C.Recomm, C.ActionRecomm,C.TotalRecomm, FROM (A INNER JOIN C ON A.CodeNo = C.CodeNo) INNER JOIN B ON A.CodeNo = B.CodeNo;
For a single record in table A there may be many records in table B. The above query repeats all the fields other than that in table B. ie., If for a record in A, there are 3 records in B, then the answer of the above query is 3 records with difference only in the B fields.
What is that I am missing? I tried some WHERE clauses, but then the query does not run. Any help is highly appreciated.
Thanks in advance for your time!
SELECT A.CodeNo, A.District, A.Agency, A.Address, A.Nature, A.RegNo, A.RegPlace, A.RegDate, A.RegCert, A.MoA, A.Audit, A.Contact, A.Pin, A.Phone, A.PastExp, B.Year, B.Grant, B.UtilCert, B.Report, B.AccState, A.Awareness AS A_Awareness, A.Action AS A_Action, C.Awareness AS C_Awareness, C.Action AS C_Action, C.Total, C.Recomm, C.ActionRecomm,C.TotalRecomm, FROM (A INNER JOIN C ON A.CodeNo = C.CodeNo) INNER JOIN B ON A.CodeNo = B.CodeNo;
For a single record in table A there may be many records in table B. The above query repeats all the fields other than that in table B. ie., If for a record in A, there are 3 records in B, then the answer of the above query is 3 records with difference only in the B fields.
What is that I am missing? I tried some WHERE clauses, but then the query does not run. Any help is highly appreciated.
Thanks in advance for your time!