Skip to content
Advertisement

I am trying to Join Xn tables on SQL and I am getting a syntax error

I tried this

(Select o.OrderId,o.CustomerID, p.ProductName 
from OrderDetails od
inner join 
Products p on od.ProductID = p.ProductID inner join
Orders o on od.OrderID = o.OrderID)

Inner join Customers c on c.CustomerID = o.CustomerID

I got this error message Error 1: could not prepare statement (1 near “(“: syntax error)

I also tried this and another error

Select *
From

(select od.OrderID,p.ProductID,p.ProductName
from Products p
inner join OrderDetails od
on p.ProductID = od.ProductID)

inner join 

(select c.Country,c.CustomerID,o.OrderID
from Customers c
inner join Orders o
on c.CustomerID = o.CustomerID)

on o.OrderID = od.OrderID;

I got this error message Error 1: could not prepare statement (1 no such column: o.OrderID)

Advertisement

Answer

I”m not sure where those parentheses are coming from. The error message is pretty clear: Most select statements do not start with an open paren.

This should do what you want:

Select o.OrderId, o.CustomerID, p.ProductName, c.Country
from OrderDetails od inner join 
     Products p
     on od.ProductID = p.ProductID inner join
     Orders o
     on od.OrderID = o.OrderID Inner join
     Customers c
     on c.CustomerID = o.CustomerID
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement