【发布时间】:2016-05-27 16:50:29
【问题描述】:
我有Customers 表,我想提取最短和最长的客户姓名
我是这样做的:
(SELECT TOP 1 CustomerName, LEN(CustomerName) AS LENGTH FROM Customers ORDER BY LEN(CustomerName) ASC, CustomerName ASC)
UNION
(SELECT TOP 1 CustomerName, LEN(CustomerName) AS LENGTH FROM Customers ORDER BY LEN(CustomerName) DESC, CustomerName ASC)
我得到两个正确的结果,但现在我想按LENGTH 列对它们进行排序。我试过这个:
SELECT * FROM
(
(SELECT TOP 1 CustomerName, LEN(CustomerName) AS LENGTH FROM Customers ORDER BY LEN(CustomerName) ASC, CustomerName ASC)
UNION
(SELECT TOP 1 CustomerName, LEN(CustomerName) AS LENGTH FROM Customers ORDER BY LEN(CustomerName) DESC, CustomerName ASC)
)
ORDER BY LEN(CustomerName) ASC
但它给了我这个错误:Syntax error in JOIN operation.
我该怎么做?
【问题讨论】:
-
我什至在你分享的源代码中都没有看到
JOIN
标签: mysql sql select join union