订购两个meged查询

时间:2016-05-27 16:50:29

标签: mysql sql select join union

我有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.

我该怎么做?

1 个答案:

答案 0 :(得分:1)

Function CategoryToImpact([string]$Category) {
    Switch ($Category) {
        'Production' {
            [System.Management.Automation.ConfirmImpact]::High
            break
        }
        'Test' {
            [System.Management.Automation.ConfirmImpact]::Medium
            break
        }
        'Development' {
            [System.Management.Automation.ConfirmImpact]::Low
            break
        }
        default {
            [System.Management.Automation.ConfirmImpact]::None
            break
        }
    }
}

Function Remove-WebSite {
    [CmdletBinding(SupportsShouldProcess=$true<#,ConfirmImpact="Depends!"#>)]
    Param(
        [Parameter(Mandatory=$true)]
        [WebSite] $WebSite
    )

    # This doesn't work but I hope it illustrates what I'd *like* to do
    #$PSCmdLet.ConfirmImpact = CategoryToImpact($WebSite.Category)

    if ($PSCmdlet.ShouldProcess("$($WebSite.Category) site $($WebSite.Name)")) {
        Write-Host "$($WebSite.Name) was destroyed"
    }
}