SQL别名在where子句中

SQL别名在where子句中

SELECT * FROM
    (
        SELECT  DISTINCT table.column, (select count(table1.column1) from table1
        where table1.column1 = tablecolumn)
        AS exp1, (select count(table1.column3) FROm table1
        LEfT OUTER JOIN table  On table1.column1 = table.column2
        WHeRE table.column = table1.column3 AND table1.column1 <= table2.column1 AND table.column = 23206) AS exp2
        FROM table1
        WHERE table1.column1 = something and .......
    ) expVys
    WHERE exp1 >= exp2 ORDER BY table1.name1 DESC

最佳答案

您可以定义CTE:

;WITH MyCTE AS
(
 SELECT  DISTINCT table.column, ((select count(table.column) from table
 where table.column = tablecolumn))
     AS exp1, ((select count(table.column3) FROm table
     LEfT OUTER JOIN table  On table.column = table.column2   WHeRE table.column = table.column3 AND table.column <= column
     AND table.column = 23206)) AS exp2
     FROM table
)

SELECT *
FROM   MyCTE
WHERE  exp1 >= exp2


我不知道这对您的评论是否有帮助,但是您可以将其全部包装在SELECT中:

SELECT *
FROM
(
 SELECT  DISTINCT table.column, ((select count(table.column) from table
 where table.column = tablecolumn))
     AS exp1, ((select count(table.column3) FROm table
     LEfT OUTER JOIN table  On table.column = table.column2   WHeRE table.column = table.column3 AND table.column <= column
     AND table.column = 23206)) AS exp2
     FROM table
) TableAlias
WHERE exp1 >= exp2

关于mysql - SQL别名在where子句中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19133358/

10-12 07:27