我有一個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