MySQL 订单条款错误

MySQL ORDER CLAUSE ERROR

我收到一条错误消息: "Unknown column 'story2.time' in 'order clause'"

我的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());

如果您能帮助执行以下查询,我们将不胜感激。谢谢!

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

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

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

联合结果的列名设置为时间,所以按时间排序即可。