Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
130 views
in Technique[技术] by (71.8m points)

sql - Why am I getting the error: CASE: syntax error

Why am I getting the error: ' CASE: syntax error' ? Attempting to retrieve records in sqlite3.

SELECT c.Region, c.City, c.PostalCode
CASE 
WHEN c.Region = e.Region THEN c.Region
        CASE
        WHEN c.City = e.City THEN c.City
            CASE
     WHEN c.PostalCode = e.PostalCode THEN c.PostalCode
     ELSE 'Null'
     END
            WHEN c.PostalCode = e.PostalCode THEN c.PostalCode
            ELSE 'Null'
            END
        ELSE 'Null'
        END
ELSE 'Null'
END
FROM customers c LEFT JOIN orders o ON c.CustomerID = o.CustomerID INNER JOIN employees e ON o.EmployeeID = e.EmployeeID;
question from:https://stackoverflow.com/questions/65932959/why-am-i-getting-the-error-case-syntax-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need a single case expression as follows:

CASE 
WHEN c.Region = e.Region THEN c.Region
WHEN c.City = e.City THEN c.City
WHEN c.PostalCode = e.PostalCode THEN c.PostalCode
END

This assumes that all the columns in THEN has same data type.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...