0

我需要按 ASC 顺序检索最后 3 行,所以这是最终查询:

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

4

2 回答 2

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
于 2013-03-16T17:10:59.007 回答
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;
于 2013-03-16T17:23:58.637 回答