Referring to a Column Alias in a WHERE Clause

后端 未结 9 2034
你的背包
你的背包 2020-11-22 05:38
SELECT logcount, logUserID, maxlogtm
   , DATEDIFF(day, maxlogtm, GETDATE()) AS daysdiff
FROM statslogsummary
WHERE daysdiff > 120

I get

相关标签:
9条回答
  • 2020-11-22 05:53
    SELECT
       logcount, logUserID, maxlogtm,
       DATEDIFF(day, maxlogtm, GETDATE()) AS daysdiff
    FROM statslogsummary
    WHERE ( DATEDIFF(day, maxlogtm, GETDATE() > 120)
    

    Normally you can't refer to field aliases in the WHERE clause. (Think of it as the entire SELECT including aliases, is applied after the WHERE clause.)

    But, as mentioned in other answers, you can force SQL to treat SELECT to be handled before the WHERE clause. This is usually done with parenthesis to force logical order of operation or with a Common Table Expression (CTE):

    Parenthesis/Subselect:

    SELECT
       *
    FROM
    (
       SELECT
          logcount, logUserID, maxlogtm,
          DATEDIFF(day, maxlogtm, GETDATE()) AS daysdiff
       FROM statslogsummary   
    ) as innerTable
    WHERE daysdiff > 120
    

    Or see Adam's answer for a CTE version of the same.

    0 讨论(0)
  • 2020-11-22 05:57

    You could refer to column alias but you need to define it using CROSS/OUTER APPLY:

    SELECT s.logcount, s.logUserID, s.maxlogtm, c.daysdiff
    FROM statslogsummary s
    CROSS APPLY (SELECT DATEDIFF(day, s.maxlogtm, GETDATE()) AS daysdiff) c
    WHERE c.daysdiff > 120;
    

    DBFiddle Demo

    Pros:

    • single definition of expression(easier to maintain/no need of copying-paste)
    • no need for wrapping entire query with CTE/outerquery
    • possibility to refer in WHERE/GROUP BY/ORDER BY
    • possible better performance(single execution)
    0 讨论(0)
  • 2020-11-22 06:01

    For me, the simplest way to use ALIAS in WHERE class is to create a subquery and select from it instead.

    Example:

    WITH Q1 AS (
        SELECT LENGTH(name) AS name_length,
        id,
        name
        FROM any_table
    )
    
    SELECT id, name, name_length form Q1 where name_length > 0
    

    Cheers, Kel

    0 讨论(0)
  • 2020-11-22 06:02

    HAVING works in MySQL according to documentation:

    The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

    0 讨论(0)
  • 2020-11-22 06:11

    If you want to use the alias in your WHERE clause, you need to wrap it in a sub select, or CTE:

    WITH LogDateDiff AS
    (
       SELECT logcount, logUserID, maxlogtm
          , DATEDIFF(day, maxlogtm, GETDATE()) AS daysdiff
       FROM statslogsummary
    )
    SELECT logCount, logUserId, maxlogtm, daysdiff
    FROM LogDateDiff
    WHERE daysdiff > 120
    
    0 讨论(0)
  • 2020-11-22 06:16

    How about using a subquery(this worked for me in Mysql)?

    SELECT * from (SELECT logcount, logUserID, maxlogtm
       , DATEDIFF(day, maxlogtm, GETDATE()) AS daysdiff
    FROM statslogsummary) as 'your_alias'
    WHERE daysdiff > 120
    
    0 讨论(0)
提交回复
热议问题