Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Every derived table must have its own alias error

Tags:

I get that error when running the following query:

SELECT MAX( DateTime )
FROM (
(
    SELECT DateTime
    FROM Class_Searches
)
UNION ALL (
    SELECT DateTime
    FROM Book_Searches
)
)
WHERE User_Email = '[email protected]'
AND DateTime > NOW( ) - INTERVAL 30 DAY 

I know I need to add Aliases but I'm not sure where

like image 975
bgcode Avatar asked Aug 08 '10 20:08

bgcode


People also ask

How do you solve the error Every derived table must have its own alias?

The short answer is you need to give your subqueries an alias in your SELECT statement. Add an alias after the closing bracket of the FROM clause subquery.

What does every derived table must have its own alias?

This error is caused by the fact that you basically generate a new table with your subquery for the FROM command. That's what a derived table is, and as such, it needs to have an alias (actually a name reference to it).

What is error 1248 in mySQL?

mySQL error: #1248 - Every derived table must have its own alias.

What is a derived table?

A derived table is an expression that generates a table within the scope of a query FROM clause. For example, a subquery in a SELECT statement FROM clause is a derived table: SELECT ... FROM (subquery) [AS] tbl_name ...


2 Answers

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 = '[email protected]'
  AND DateTime > NOW( ) - INTERVAL 30 DAY

  UNION ALL

  SELECT DateTime
  FROM Book_Searches
  WHERE User_Email = '[email protected]'
  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 = '[email protected]'
AND DateTime > NOW( ) - INTERVAL 30 DAY
like image 145
Guffa Avatar answered Sep 25 '22 15:09

Guffa


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 = '[email protected]'
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 = '[email protected]'
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.

like image 30
zebediah49 Avatar answered Sep 23 '22 15:09

zebediah49