0

最後の 3 行を取得する必要がありますが、ASC 順であるため、最終的なクエリは次のとおりです。

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 に答える