I have a database with a few tables that contains articles, users and other info for a site I am creating.
At the moment I am working out all the possible SQL statements I could need before I start the PHP.
I have done quite well so far, considering my lack of SQL knowledge, but I have hit a snag.
I have two tables user and article. I have an SQL statement that will give me the most recent article of the whole site and it appears to work properly:
The output is:
Now, I would like statements that grab the most recent article per section and per user.
So I came up with this:
The output is:
This appeared to work at first, but when I actually checked I realised the article title was wrong for the second record returned as you can see by the contents of the article table:
The SQL statement correctly gets the latest date per section, but seems to choose the earliest article per section [and associated user_name]. I am not sure how to fix it!
Any help would be great
At the moment I am working out all the possible SQL statements I could need before I start the PHP.
I have done quite well so far, considering my lack of SQL knowledge, but I have hit a snag.
I have two tables user and article. I have an SQL statement that will give me the most recent article of the whole site and it appears to work properly:
Code:
SELECT user_name, article_title, article_date, article_section
FROM user, article
WHERE user_id = article_user
AND article_date = (SELECT MAX(article_date) FROM article);
Code:
+-----------+----------------------------+---------------------+-----------------+
| user_name | article_title | article_date | article_section |
+-----------+----------------------------+---------------------+-----------------+
| pspain | New Folding@Home V6 Client | 2007-10-04 00:52:00 | 1 |
+-----------+----------------------------+---------------------+-----------------+
So I came up with this:
Code:
SELECT user_name, article_title, MAX(article_date), article_section
FROM user, article
WHERE user_id = article_user GROUP BY article_section;
Code:
+-----------+--------------------------------------------------+---------------------+-----------------+
| user_name | article_title | MAX(article_date) | article_section |
+-----------+--------------------------------------------------+---------------------+-----------------+
| pspain | New Folding@Home V6 Client | 2007-10-04 00:52:00 | 1 |
| pspain | Llyn Ogwen to Pen Yr Ole Wen | 2007-10-01 19:41:28 | 3 |
| pspain | The new 'Age of the Five' books by Trudi Canavan | 2007-10-01 19:46:06 | 9 |
+-----------+--------------------------------------------------+---------------------+-----------------+
Code:
+--------------------------------------------------+---------------------+
| article_title | article_date |
+--------------------------------------------------+---------------------+
| Llyn Ogwen to Pen Yr Ole Wen | 2007-10-01 19:34:59 |
| Pen-Y-Pass to Elidir Fawr | 2007-10-01 19:39:09 |
| Crowden to Black Hill | 2007-10-01 19:41:28 |
| The new 'Age of the Five' books by Trudi Canavan | 2007-10-01 19:46:06 |
| New Folding@Home V6 Client | 2007-10-04 00:52:00 |
+--------------------------------------------------+---------------------+
Any help would be great
