How to join multiple tables effectively SQL

37 views Asked by At

I am encountering a syntax error when attempting to join tables in my SQL query. Here's the context:

I have the following tables

  • tblGeneral (ID, DestinationID, OriginID)
  • tblDestinations (DestinationID, DestinationName)
  • tblPrices (OriginID, OriginName, Africa, Asia, China)

I want to use the DestinationID in tblGeneral, to get the relevant price from tblPrices Eg. a sample row of tblDestinations:

DestinationID DestinationName
1 Africa

I want to get the DestinationName and use that to reference the column in tblPrices (in this case Africa)

I have tried the following query:

SELECT 
    tblGeneral.ID AS ID, tblDestinations.DestinationName AS Destination, tblPrices.OriginName AS Origin,
    SWITCH(
        tblDestinations.DestinationName = "Africa", tblPrices.Africa,
        tblDestinations.DestinationName = "Asia", tblPrices.Asia,
        tblDestinations.DestinationName = "China", tblPrices.China
    ) AS Price
FROM 
    tblGeneral
INNER JOIN 
    tblDestinations ON tblGeneral.DestinationID = tblDestinations.DestinationID
INNER JOIN 
    tblPrices ON tblGeneral.OriginID = tblPrices.OriginID;

I expect something like this as output:

ID Destination Origin Price
1 Africa Brazil $57.90

However I get Syntax error - Missing operator. I've tried adjusting the join conditions and reviewing the query syntax, but I'm unable to resolve the issue.

Any help would be greatly appreciated!

0

There are 0 answers