Question

I am writing a query to join 3 tables, I have the following tables:

  • Apn [ID, QualHolderID, RecvDate, Barcode]
  • QualHolder [ID, Surname, FullName, DOB, ResCountryID, Gender]
  • Country [ID, Name]
  • Gender [ID, Name]

I wrote the following SQL statement to retrieve the data:

SELECT 
    a.QualHolderID, b.FullName, b.Surname, d.Name, b.DOB, b.ResCountryID, a.RecvDate
FROM
    dbo.Apn a 
INNER JOIN 
    dbo.QualHolder b ON a.QualHolderID = b.ID 
JOIN
    dbo.QualHolder c 
INNER JOIN 
    dbo.Gender d ON c.Gender = d.ID 
WHERE
    b.ResCountryID = 48

But now I get the following error:

Msg 102, Level 15, State 1, Line 9
Incorrect syntax near 'ID'.

Was it helpful?

Solution 2

You don't need to join dbo.QualHolder table two times. Try it this way:

SELECT a.qualholderid, 
       b.fullname, 
       b.surname, 
       d.name, 
       b.dob, 
       b.rescountryid, 
       a.recvdate 
FROM   dbo.apn a 
       INNER JOIN dbo.qualholder b 
               ON a.qualholderid = b.id 
       INNER JOIN dbo.gender d 
               ON b.gender = d.id 
WHERE  b.rescountryid = 48 

OTHER TIPS

I think that the correct query is:

 SELECT a.QualHolderID, b.FullName, b.Surname, d.Name, b.DOB, b.ResCountryID, a.RecvDate
 FROM dbo.Apn a 
 INNER JOIN dbo.QualHolder b ON a.QualHolderID = b.ID 
 INNER JOIN dbo.Gender c ON b.Gender = c.ID 
 WHERE b.ResCountryID = 48

Your problem is here = b.ID JOIN use following query and execute.

SELECT a.QualHolderID, b.FullName, b.Surname, d.Name, b.DOB, b.ResCountryID, a.RecvDate
FROM (dbo.Apn a INNER JOIN dbo.QualHolder b ON a.QualHolderID = b.ID) 
INNER JOIN dbo.Gender d ON b.Gender = d.ID 
WHERE b.ResCountryID = 48
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top