Every derived table must have its own alias error

后端 未结 2 1707
迷失自我
迷失自我 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:12

    An alias is when you rename something, like SELECT t.time from table t, t is the alias for that table. In this case, you need to give an alias to the tables generated by the subqueries:

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

    That still won't work though, because you don't have a User_Email column returned from the UNION. Thus, try:

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

    It's possible that that still won't work right because of the UNION syntax, but at least it's a lot closer.

提交回复
热议问题