2013-03-16 23 views
0

我需要找回過去的3行,但在ASC順序,所以這裏的最終查詢:SELECT語句的選擇:每個派生的表必須有它自己的別名

SELECT c.comment_id,c.comment_message,c.comment_date,u.user_id,u.first_name,u.last_name,p.profile_picture_path 
FROM 
    (SELECT c.comment_id,c.comment_message,c.comment_date,u.user_id,u.first_name,u.last_name,p.profile_picture_path 
     FROM posts_comments c,users u,users_profile_pictures p 
     WHERE c.user_id = u.user_id AND u.user_id = p.user_id AND c.post_id = '82' 
     ORDER BY c.comment_date DESC 
     LIMIT 3) 
ORDER BY c.comment_date ASC 

我知道這裏有什麼問題,我收到此錯誤:Every derived table must have its own alias。如何從Select語句中選擇列時,它們由相應的表指出?意思是,我該如何選擇c.comment_id?在「字段list'```未知列 'c.comment_id':

回答

4
SELECT comment_id, 
     comment_message, 
     comment_date, 
     user_id, 
     first_name, 
     last_name, 
     profile_picture_path 
FROM (
     SELECT c.comment_id, 
       c.comment_message, 
       c.comment_date, 
       u.user_id, 
       u.first_name, 
       u.last_name, 
       p.profile_picture_path 
     FROM posts_comments c, 
       users u, 
       users_profile_pictures p 
     WHERE c.user_id = u.user_id 
       AND u.user_id = p.user_id 
       AND c.post_id = '82' 
     ORDER BY c.comment_date DESC 
     LIMIT 3 
    ) subA -- <<== put alias here 
      -- the purpose of the alias is to supply an identification 
      -- for the subquery 
ORDER BY comment_date ASC 
+0

現在,我得到這個(' – 2013-03-16 17:17:40

+0

遺憾的錯字,再次嘗試更新的查詢'C'無效。因爲它包含在子查詢中 – 2013-03-16 17:19:03

+0

It Worked':D'但爲什麼它需要一個別名? – 2013-03-16 17:20:40

1
SELECT x.comment_id 
     , x.comment_message 
     , x.comment_date 
     , x.user_id 
     , x.first_name 
     , x.last_name 
     , x.profile_picture_path 
    FROM 
     (SELECT c.comment_id 
      , c.comment_message 
      , c.comment_date 
      , u.user_id 
      , u.first_name 
      , u.last_name 
      , p.profile_picture_path 
      FROM posts_comments c 
      JOIN users u 
      ON u.user_id = c.user_id 
      JOIN users_profile_pictures p 
      ON p.user_id = u.user_id 
     WHERE c.post_id = 82 
     ORDER 
      BY c.comment_date DESC 
     LIMIT 3 
    ) x 
    ORDER 
    BY x.comment_date ASC; 
相關問題