我有一个articles
表和categories
表是这样的:如何获得每个类别中最最近的一篇文章日期
articles
-----------------------------------------------
| id | datetime | title | categoryid | status |
-----------------------------------------------
categories
-------------
| id | name |
-------------
我需要获得最新的文章从每个类别日期的选择,像:
result
----------------------------------------------------------
| category id | category name | most recent article date |
----------------------------------------------------------
结果必须包含每个类别的所有类别+最近的文章(状态= 0)日期。
它必须产生酷似select * from categories
PLUS显示类别的最最近搜索文章的datetime
但只有当文章状态为0新行。
这是明确的吗?
我需要的原因是因为我试图制作一个动态站点地图,而且我需要每个类别的最新文章的时间在<lastmod>
标签上显示类别列表。
$sql = mysql_query(".....?......");
while ($string = mysql_fetch_array($sql)){?>
<url>
<loc>http://www.url.com/<?echo $string['id'];?>/<?echo generate_seo_link($string['name']);?>/</loc>
<priority>0.5</priority>
<changefreq>daily</changefreq>
<lastmod><? echo date("d/m/Y H:i:s", strtotime($string['date'])); ?></lastmod>
</url>
<?}
你试过了什么SQL语句?可能重复:http://stackoverflow.com/questions/3869426/mysql-join-most-recent-matching-record-from-one-table-to-another – Jon 2013-03-21 01:40:31