从不同的类别中选择文章

时间:2011-08-10 10:38:33

标签: mysql

我有一个表格,其中包含一些名为“文章”的文章,其中包含以下列和行

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

2 个答案:

答案 0 :(得分:0)

这样的东西
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
                )

答案 1 :(得分:0)

只需将您的限制从6更改为2,您的代码就可以正常工作:

SELECT * FROM articles WHERE category IN ('1', '2', '3') ORDER BY date DESC LIMIT 2;