小编典典

每个派生表必须具有其自己的别名错误

sql

运行以下查询时出现该错误:

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

我知道我需要添加别名,但是我不确定在哪里


阅读 121

收藏
2021-03-23

共1个答案

小编典典

您需要为子查询添加别名,并且需要将条件应用于您合并的两个查询:

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

或返回数据,以便您可以在外部查询中应用条件:

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
2021-03-23