0

我有这个代码,但它不工作。在我使用最新评论进行查询后,我想颠倒顺序。

SELECT * 
FROM( 
    SELECT users.id, users.nome, users.email,users.foto, comments.content
    FROM users, comments 
    WHERE comments.posts_id=? AND comments.users_id=users.id
    ORDER BY comments.id DESC LIMIT 5) 
AS dummy 
ORDER BY comments.timecomment ASC
4

3 回答 3

1

comments.timecomment在内部 SELECT 查询中似乎没有选择您希望排序的字段...此外,如果字段名称是唯一的,则在外部查询中可能不需要显式引用表名称。

怎么样:

SELECT * 
FROM( 
    SELECT users.id, users.nome, users.email,users.foto, comments.content,
    comments.timecomment
    FROM users, comments 
    WHERE comments.posts_id=? AND comments.users_id=users.id
    ORDER BY comments.id DESC LIMIT 5) 
AS dummy 
ORDER BY timecomment ASC
于 2013-05-19T16:36:28.893 回答
0

如果您的目标只是输出找到的评论,请在 php 中将其反转:

SELECT users.id, users.nome, users.email,users.foto, comments.content
  FROM users, comments 
  WHERE comments.posts_id=? AND comments.users_id=users.id
  ORDER BY comments.id DESC LIMIT 5

$comments = array_reverse($comments);

于 2013-05-19T16:27:17.933 回答
0

看起来你只需要给你的外部查询一些东西(cid)来排序,然后做你已经在做的事情;

SELECT * 
FROM (SELECT users.id, users.nome, users.email,users.foto, 
             comments.content, comments.id cid
      FROM users, comments 
      WHERE comments.posts_id=1 AND comments.users_id=users.id
      ORDER BY comments.id DESC LIMIT 5) AS dummy
ORDER BY cid ASC
于 2013-05-19T16:28:29.720 回答