Question

I'm making a join between three tables and while these joins work without being combined, when I add them together it tells me "Incorrect syntax near 'Person'."

This is my query:

SELECT [AddressLine1], [FirstName], [LastName]
FROM
[Person].[BusinessEntityAddress] join [Person].[Address] 
on ([Person].[BusinessEntityAddress].[AddressID]=[Person].[Address].[AddressID])
[Person].[Person] 
join [Person].[BusinessEntityAddress] 
on ([Person].[Person].[BusinessEntityID]=[Person].[BusinessEntityAddress].[BusinessEntityID])
WHERE FirstName= 'Terri' AND LastName= 'Duffy'

The issue is on the last line [Person].[Person] join [Person].[BusinessEntityAddress]...

Any help would be greatly appreciated. And I can answer questions to make it more specific.

Was it helpful?

Solution

SELECT   [Person].[Address].[AddressLine1]
       , [Person].[Person].[FirstName]
       , [Person].[Person].[LastName]
FROM [Person].[BusinessEntityAddress] join [Person].[Address] 
on [Person].[BusinessEntityAddress].[AddressID]=[Person].[Address].[AddressID]
JOIN [Person].[Person] 
ON [Person].[Person].[BusinessEntityID] = [Person].[BusinessEntityAddress].[BusinessEntityID]
WHERE FirstName= 'Terri' AND LastName= 'Duffy'

--or using Alias makes it a lot cleaner and easier to read

SELECT   PA.[AddressLine1]
       , PP.[FirstName]
       , PP.[LastName]
FROM  [Person].[BusinessEntityAddress] PB 
INNER JOIN 
      [Person].[Address] PA
ON    PB.[AddressID] = PA.[AddressID]
INNER JOIN  
      [Person].[Person] PP
ON    PP.[BusinessEntityID] = PB.[BusinessEntityID]
WHERE PP.[FirstName] = 'Terri' 
AND   PP.[FirstName] = 'Duffy'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top