MySQL ORDER CLAUSE ERROR

时间:2015-11-04 03:25:25

标签: mysql join union

我收到的错误是:  “'order clause'中的未知栏'story2.time'”

我的SQL语句是:

mysql_query("SELECT headline, story2.time FROM story2 WHERE username='Michael' UNION
SELECT headline, story2.time FROM story2 JOIN subscriptions WHERE subscriptions.subpaperid = story2.artnewsid AND subscriptions.papernameurl =  story2.papernameurl AND subscriptions.username = 'Michael' UNION 
SELECT headline, story2.time FROM story2 JOIN bookmark WHERE bookmark.writername = story2.username AND bookmark.articleid = story2.random AND bookmark.username = 'Michael'
ORDER BY story2.time DESC LIMIT 0,25") or die(mysql_error());

任何有关进行以下查询工作的帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:2)

您无法在table中使用order by别名。所以,就这样做:

SELECT headline, story2.time
FROM story2
WHERE username='Michael'
UNION
SELECT headline, story2.time
FROM story2 JOIN
     subscriptions
WHERE subscriptions.subpaperid = story2.artnewsid AND subscriptions.papernameurl =  story2.papernameurl AND subscriptions.username = 'Michael'
UNION 
SELECT headline, story2.time
FROM story2 JOIN
     bookmark
WHERE bookmark.writername = story2.username AND bookmark.articleid = story2.random AND bookmark.username = 'Michael'
ORDER BY time DESC
LIMIT 0, 25

表名仅在union中每个子查询的范围内。顺便说一句,你应该使用union all,除非你故意要承担删除重复项的开销。

答案 1 :(得分:1)

联合结果的列名设置为时间,因此只是按时间排序。