你能帮助我显示每组的最新数据吗?

时间:2018-01-05 10:02:18

标签: mysqli

我有这个数据表:

table1

id  category
-------------
1     a
2     b
3     c

table2

id heading category_id
----------------------
1   name      1
2   adddress  2
3   phone     3
4   email     1

我想对此表进行分组,并显示我使用的以下查询的最新数据:

SELECT news.id,news.image,news.heading,news.description,
        news.date,news.category_id,categories.category 
FROM `news` 
    INNER JOIN categories On news.category_id=categories.id 
group by category_id 

但我没有得到我输入的最新数据。

1 个答案:

答案 0 :(得分:0)

尝试以下查询:

SELECT * 
FROM table2 AS tb2 LEFT JOIN table1 AS tb1 ON tb2.category_id = tb1.id 
ORDER BY tb1.id 
GROUP BY tb2.category_id