0

是否可以每个状态仅选择 2 个问题。我知道 mysql 不支持对子查询的限制,但我已经看到了我无法弄清楚如何用于此查询的解决方法。这是一个查询问题还是我必须使用额外的查询?我正在尝试为每个状态选择 2 行。

没有LIMIT 2下面的查询可以工作,但它有太多的结果,因此需要限制它。

SELECT id FROM questions WHERE status_id = c.status_id LIMIT 2

询问:

  ...
    FROM questions q, users u, users lu, tags t, question_tags tt, connection c
   WHERE q.user_id = u.id
     AND tt.tag_id = t.id
     AND tt.question_id = q.id
     AND c.user_id = 100
     AND q.last_user = lu.id
     AND q.id IN (SELECT id FROM questions WHERE status_id = c.status_id LIMIT 2)
GROUP BY q.id
ORDER BY q.date DESC
4

1 回答 1

0
...
    FROM questions q, users u, users lu, tags t, question_tags tt, connection c
   WHERE q.user_id = u.id
     AND tt.tag_id = t.id
     AND tt.question_id = q.id
     AND c.user_id = 100
     AND q.last_user = lu.id
     AND q.id IN (SELECT MAX(id) FROM questions WHERE status_id = c.status_id, 
                  SELECT MAX(id) FROM questions WHERE status_id = c.status_id AND id < 
                        (SELECT MAX(id) FROM questions WHERE status_id = c.status_id))
GROUP BY q.id
ORDER BY q.date DESC

丑陋,我知道,但它应该工作。

于 2012-12-05T12:47:55.917 回答