mysql错误表不能用于全局顺序子句

时间:2014-02-05 23:56:26

标签: mysql join mysqli union

(SELECT dtable.* FROM app_detailsvvv as dtable INNER JOIN new_apps ON 
new_apps.trackId=dtable.trackId WHERE primaryGenreName='Games' AND composed='1' AND
new_apps.top>0) UNION (SELECT * FROM app_detailsvvv as dtable WHERE
primaryGenreName='Games') ORDER BY new_apps.top ASC, trackName ASC LIMIT 12

使用该查询我收到此错误:

#1250 - Table 'new_apps' from one of the SELECTs cannot be used in global ORDER clause

在第二个查询中没有使用new_apps,我们的想法是从同一个表中获得最高排名,该表也列出了new_apps表中的id

1 个答案:

答案 0 :(得分:4)

作为文档says

  

这种ORDER BY不能使用包含表名的列引用(即tbl_name.col_name格式的名称)。而是在第一个SELECT语句中提供列别名,并引用ORDER BY中的别名。

所以像这样重写它

(
    SELECT dtable.*, new_apps.top as t1
    FROM app_detailsvvv as dtable
    INNER JOIN new_apps ON new_apps.trackId=dtable.trackId
    WHERE primaryGenreName='Games' AND composed='1' AND new_apps.top > 0
)
UNION
(
    SELECT *, 0
    FROM app_detailsvvv as dtable
    WHERE primaryGenreName='Games'
)
ORDER BY t1 ASC, trackName ASC
LIMIT 12