我的Sql查询为每个类别选择2条记录

时间:2016-01-21 09:26:12

标签: php mysql

我有2张桌子。 1.新闻表2.类别表。

在新闻表中我有id,title,category id 在类别表中,我有id和类别名称。

在新闻表中有许多新闻项目,每个类别都有多个新闻项目。

我需要的是从新闻表中为每个类别选择2条记录,我还需要加入这两个表,以便我可以从类别表中获取类别的名称。 我尝试使用以下查询

SELECT * FROM (
    SELECT
            news.id, news.fk_lookups_category, lookups_category.name, news.title, news.description,news.datetime,lookups_category.priority
        FROM news
            JOIN lookups_category ON news.fk_lookups_category=lookups_category.id
        WHERE
            news.isPublished='1' and news.datetime >= ('today' - INTERVAL 7 day) order by datetime DESC
) as newitem order by priority

0 个答案:

没有答案
相关问题