我有一个表格,里面有一些名为"articles"的文章,列&行
id - category - title - content - date
1 - 1 - my title - my content - time here
2 - 2 - my title - my content - time here
3 - 1 - my title - my content - time here
4 - 1 - my title - my content - time here
5 - 3 - my title - my content - time here
6 - 1 - my title - my content - time here
7 - 2 - my title - my content - time here
8 - 3 - my title - my content - time her
9 - 4 - my title - my content - time here
10 - 4 - my title - my content - time here
我需要从类别1、2和3中选择2个不同的文章
目前为止我的代码是:
SELECT * FROM articles WHERE category IN ('1', '2', '3') ORDER BY date DESC LIMIT 6
SELECt *
FROm articles a
WHERE category IN ('1', '2', '3')
AND ID IN (
SELECT ID
FROM articles
WHERE category = a.category
ORDER BY date DESC
LIMIT 2
)
只要把你的限制从6个改为2个,你的代码就可以正常工作了:
SELECT * FROM articles WHERE category IN ('1', '2', '3') ORDER BY date DESC LIMIT 2;