Every derived table must have its own alias error

后端 未结 2 1702
迷失自我
迷失自我 2021-01-07 20:42

I get that error when running the following query:

SELECT MAX( DateTime )
FROM (
(
    SELECT DateTime
    FROM Class_Searches
)
UNION ALL (
    SELECT DateT         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-07 21:06

    You need an alias for the subquery, and you need to apply the conditions either to both queries that you union:

    SELECT MAX(DateTime)
    FROM (
    
      SELECT DateTime
      FROM Class_Searches
      WHERE User_Email = 'bla@blah.com'
      AND DateTime > NOW( ) - INTERVAL 30 DAY
    
      UNION ALL
    
      SELECT DateTime
      FROM Book_Searches
      WHERE User_Email = 'bla@blah.com'
      AND DateTime > NOW( ) - INTERVAL 30 DAY
    
    ) AS x
    

    or return data so that you can apply the condition in the outer query:

    SELECT MAX(DateTime)
    FROM (
    
      SELECT DateTime, User_Email
      FROM Class_Searches
    
      UNION ALL
    
      SELECT DateTime, User_Email
      FROM Book_Searches
    
    ) AS x
    WHERE User_Email = 'bla@blah.com'
    AND DateTime > NOW( ) - INTERVAL 30 DAY
    

提交回复
热议问题