2012-11-27 39 views
1

可能重複:
every derived table must have its own aliasMYSQL顯示錯誤::每個派生的表必須有它自己的別名OM PHP MYSQL

暈我試圖得到一個表中記錄拋棄它可以通過組合一個表的兩個結果來顯示結果..這裏在我的表'updatez'中,我存儲了來自用戶(朋友表存儲朋友的用戶可訪問性)和換器(換器表存儲用戶對換器的可訪問性)的所有更新這張桌子.... 在這裏我是灣泰德通過排序創建時間和次序0,5來顯示所有的朋友和轉換更新(如果他們批准)...

還有沒有其他更簡單的方法來做到這一點我試圖

SELECT * 
FROM (
     SELECT M.updt_id AS updt_id, 
       M.user_id_fk AS user_id_fk, 
       M.updates AS updates, 
       M.created AS created, 
       M.uploads AS uploads 
     FROM updatez M 
     WHERE updatez.user_id_fk = friends.friendA 
       AND friends.friendB = $USER 

     UNION 

     SELECT A.updt_id AS updt_id, 
       changer_id_fk AS changer_id_fk, 
       A.updates AS updates, 
       A.created AS created, 
       A.uploads AS uploads 
     FROM updatez A 
     WHERE updatez.changer_id_fk = changer.changer_id 
       AND changer.user_id = $USER 
     ) 
ORDER BY created DESC 
LIMIT 0, 5 
代碼

任何形式的幫助將感激.. thanx提前 添

回答

5

你只需要在子查詢添加ALIAS

SELECT * 
FROM (
     SELECT M.updt_id AS updt_id, 
       M.user_id_fk AS user_id_fk, 
       M.updates AS updates, 
       M.created AS created, 
       M.uploads AS uploads 
     FROM updatez M 
     WHERE updatez.user_id_fk = friends.friendA 
       AND friends.friendB = $USER 

     UNION 

     SELECT A.updt_id AS updt_id, 
       changer_id_fk AS changer_id_fk, 
       A.updates AS updates, 
       A.created AS created, 
       A.uploads AS uploads 
     FROM updatez A 
     WHERE updatez.changer_id_fk = changer.changer_id 
       AND changer.user_id = $USER 
     ) derivedTable       // -- <<< HERE 
ORDER BY created DESC 
LIMIT 0, 5 
+0

謝謝你vaery多..親愛的actully現在geeting一個錯誤,我不明白錯誤是#1054 - 未知的列''where'子句'中的'updatez.user_id_fk' – tradebel123

+0

由於你已經在updatez表上提供了一個別名,你需要使用alias.example m.user_id_fk .. –

+0

順便說一下,朋友和更換表在哪裏? –

相關問題