【问题标题】:Order two meged queries订购两个 meged 查询
【发布时间】: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


【解决方案1】:
select customername,len(customername) as namelength
from (SELECT MAX(LEN(CustomerName)) AS MAXLENGTH, MIN(LEN(CustomerName)) AS MINLENGTH 
      FROM Customers) lens
join customers c on c.len(customername) = lens.maxlength or c.len(customername) = lens.minlength
order by namelength desc, customername

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多