MySQL Error 1248 (42000): Every Derived Table Must Have Its Own Alias

MYSQL ERROR 1248 (42000): Every derived table must have its own alias

It means exactly what it says - each derived table must have an alias. SELECT a.* FROM (SELECT ....)a

Update.
This should work for you:

SELECT xxx.* FROM 
(
SELECT ....
FROM ....
UNION
(
SELECT ....
FROM .....
LIMIT 46
)
LIMIT 50
)xxx

Error 1248 (42000): Every derived table must have its own alias

you are missing an alias on the inner query right after the LEFT OUTER JOIN

SELECT p.PlayerAlias as "Player", 
s.HiScore as "Score"
FROM Player as p INNER JOIN Score as s ON p.PlayerID = s.PlayerID
LEFT OUTER JOIN (
SELECT g.GameID
FROM Game as g
WHERE g.GameName = "Reaper"
) as g ON s.GameID = g.GameID
WHERE s.HiScore < 7000 AND s.HiScore > 4000;

SQL Error [1248] [42000]: Every derived table must have its own alias

The subquery in your query above needs an alias, that is all:

SELECT *
FROM
(
SELECT 'OfferItm9' AS OfferNo, O.DocNo, O.Stkno, O.OfferPrice, O.StartDateItm, O.EndDateItm
FROM OfferItm9 O
WHERE O.EndDateItm >= CURDATE() AND O.Active = '1' AND O.UOM = 'U'
UNION ALL
SELECT 'OfferItm8', O.DocNo, O.Stkno, O.OfferPrice, O.StartDateItm, O.EndDateItm
FROM OfferItm8 O
WHERE O.EndDateItm >= CURDATE() AND O.Active = '1' AND O.UOM = 'U'
) t;

But, I don't even see the point of the subquery, so just use:

SELECT 'OfferItm9' AS OfferNo, O.DocNo, O.Stkno, O.OfferPrice, O.StartDateItm, O.EndDateItm
FROM OfferItm9 O
WHERE O.EndDateItm >= CURDATE() AND O.Active = '1' AND O.UOM = 'U'
UNION ALL
SELECT 'OfferItm8', O.DocNo, O.Stkno, O.OfferPrice, O.StartDateItm, O.EndDateItm
FROM OfferItm8 O
WHERE O.EndDateItm >= CURDATE() AND O.Active = '1' AND O.UOM = 'U';

Note: Use UNION ALL here for performance.

What is the error Every derived table must have its own alias in MySQL?

Every derived table (AKA sub-query) must indeed have an alias. I.e. each query in brackets must be given an alias (AS whatever), which can the be used to refer to it in the rest of the outer query.

SELECT ID FROM (
SELECT ID, msisdn FROM (
SELECT * FROM TT2
) AS T
) AS T

In your case, of course, the entire query could be replaced with:

SELECT ID FROM TT2

Every derived table must have its own alias Mysql Error

The error message is clear enough. You must alias the derived table that is generated by your sub-select. So, give it an alias.

Another issue is that, in the subquery, non-aggregated column deviceType should be included in the GROUP BY clause. This change might, or might not produce the results that you do expect: if it doesn't, then you would need to provide sample data, expected results and an explanation of what you are trying to accomplish so one can help fixing the query.

SELECT minDate, deviceType, COUNT(*)
FROM (
SELECT visitorId, deviceType, MIN(sessionDate) as minDate
FROM sessions
GROUP BY visitorId, deviceType -- all non-aggregated columns in the GROUP BY clause
) t -- alias here
WHERE minDate > DATE_SUB(NOW(), INTERVAL 14 DAY)
GROUP BY minDate, deviceId

ERROR 1248 (42000): Every derived table must have its own alias , alias are present (inner query with group and having)

You are joining

ON cisT.countrycode = c2.countrycode

but countrycode column does not exist in cisT table.

How to solve ERROR 1248 (42000): Every derived table must have its own alias

The derived table needs an alias and you were missing a join condition onto it as well.

SELECT *
FROM post
LEFT JOIN post_plus
ON ( post.id = post_plus.news_id )
LEFT JOIN (SELECT DISTINCT c1.postid
FROM post_category c1
JOIN post_category c2
ON c1.postid = c2.postid
WHERE c1.categoryid IN ( 130, 3, 4, 5 )
AND c2.categoryid = 73) post_category /*<-- Missing Alias*/
ON ( post_category.postid = post.id ) /*<-- Missing Join Condition*/
WHERE approve = 1
ORDER BY fixed DESC,
date DESC
LIMIT 0, 7;


Related Topics



Leave a reply



Submit