Mysql子查询反转顺序

时间:2013-05-19 16:21:21

标签: mysql subquery

我有这个代码而且它无效。在使用最新评论进行查询后,我想颠倒订单。

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);

答案 2 :(得分: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