Every derived table must have its own alias - error from combination descending MySQL

◇◆丶佛笑我妖孽 提交于 2019-12-23 09:06:00

问题


I want to order one mysql table by two strtotime timestamps from two different columns. I've got the following mysql command:

SELECT * FROM (
(SELECT '1' AS `table`,  `vid_req_timestamp` AS `timestamp`, `title` FROM `movies` WHERE `vid_req` = '1')
UNION
(SELECT '2' AS `table`,  `ost_req_timestamp` AS `timestamp`, `title` FROM `movies` WHERE `ost_req` = '1')
)
ORDER BY `timestamp` DESC

This gives me an error:

#1248 - Every derived table must have its own alias

I want to combine vid_req_timestamp and ost_req_timestamp and make those descending. And it's important to know where the timestamp came from (somehow).


回答1:


In this case, the derived table that requires an alias is the one that you are SELECTing * from.

Indentation helps make that clearer.

SELECT * FROM 
(
    (SELECT '1' AS `table`,  `vid_req_timestamp` AS `timestamp`, `title` FROM `movies` WHERE `vid_req` = '1')
    UNION
    (SELECT '2' AS `table`,  `ost_req_timestamp` AS `timestamp`, `title` FROM `movies` WHERE `ost_req` = '1')
) AS `some_table_name_lol_this_is_an_alias`
ORDER BY `timestamp` DESC


来源:https://stackoverflow.com/questions/9119857/every-derived-table-must-have-its-own-alias-error-from-combination-descending

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!