I have a table for news and I want to select the 3 newest things in each row, they're numbered 1 as the oldest and each data I add it will be a number higher. So how do you get the three newest things and display the them in places? (Sorry I can't think of how to phrase my question)
EDIT:
I don't have like html or anything so show the desired effect, but I can explain it. I have an SQL Table that has 4 rows. ID, TITLE, DATE, and POST. This is a news feed, and I want to display the 3 newest news "articles" on the homepage. But I don't want to just display them as lists, I want to put the title in a top div, next to the date, I want to order it by the primary key (ID) and then have the post in a div under the title and date. Here's an attempt to show you:
Title | Date
Newest news. Etc. Insert Big Paragraph of news here etc etc.
Title | Date
2nd Newest news. Etc. Insert Big Paragraph of news here etc etc.
Title | Date
3rd Newest news. Etc. Insert Big Paragraph of news here etc etc.
first, please dont use 'date' as your field name, lets say you rename it as news_date. How about this?
<?php
$query = mysql_query ("SELECT * FROM yourtable ORDER BY id DESC LIMIT 3");
$number = 1;
while ($result = mysql_fetch_array($query)) {
$id = $result['id'];
$title = $result['title'];
$news_date = $result['news_date'];
$post = $result['post'];
?>
<div name='title'><?php echo $title; ?></div> || <div name='news_date'><?php echo $news_date; ?></div>
<p>News <?php echo $number; ?></p>
<p><?php echo $post; ?></p>
<?php
$number++;
}
?>
If my understanding of your question is correct, it seems you have a table with a row identifier (numbered 1 to ... n) and you want to run a query that will bring back the 3 most recent inserted records ?
If so:
Lets assume you have a table called Books with the below structure:
Books:
- RowNumber
- Col_1
- Col_2
- ...
What you need to do is run the following SQL:
SELECT
TOP 3 *
FROM
Books
ORDER BY
RowNumber DESC
I hope this helps :)
If I am getting you correctly you can use LIMIT
and ORDER BY
Example
`SELECT columns FROM yourtable ORDER BY primary_key DESC LIMIT 0,2`