2015-11-04 70 views
1

我得到一個錯誤,指出: 「未知列‘story2.time’‘以條款’」mysql命令根據條款

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

上做任何幫助下面的查詢工作將不勝感激。謝謝!

+0

您不能使用'union'按順序引用'story2' - 它不再存在。只要刪除它 - '按時間順序desc ...' – sgeddes

回答

2

您不能在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 

表名是唯一的範圍在每union子查詢。順便說一下,除非您故意要承擔刪除重複項的開銷,否則您應該使用union all

+0

非常感謝! – stevieD

1

聯合結果的列名設置爲時間,所以按時間排序。