1
SELECT 
  (SELECT date FROM forums WHERE topic_id=f.id OR id=f.id ORDER BY id DESC LIMIT 1) as last_reply, 
  f.*, p.id as pid, p.name FROM forums f 
      INNER JOIN players p ON p.id = f.author 
  WHERE f.topic_id=0 ORDER BY f.id DESC

在子查询中,我不仅要返回date字段,还要返回author字段。我怎样才能做到这一点?

看了一个类似的帖子,但无法将其应用于我的帖子。

4

2 回答 2

2

我会这样做:

SELECT 
  (SELECT date FROM forums WHERE topic_id=f.id OR id=f.id ORDER BY id DESC LIMIT 1) as last_reply,
  (SELECT author FROM forums WHERE topic_id=f.id OR id=f.id ORDER BY id DESC LIMIT 1) as last_author,
  f.*, p.id as pid, p.name FROM forums f 
      INNER JOIN players p ON p.id = f.author 
  WHERE f.topic_id=0 ORDER BY f.id DESC

我实际上会再次重复子查询

于 2012-04-08T22:35:48.240 回答
0

也许这样的事情可以帮助你:

SELECT MAX(f.date), f.author
FROM forums f
    INNER JOIN players p ON
        p.id = f.author
WHERE f.tpoic_id = 0
GROUP BY f.author
ORDER BY f.id DESC

但是没有表格结构就很困难。

祝你好运。

于 2012-04-08T22:39:33.627 回答