在SQL表中选择最新数据? [英] Select the newest data in an SQL table?

查看:132
本文介绍了在SQL表中选择最新数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张新闻表格,我想在每行中选择3个最新的事物,它们被编号为1最旧,并且我添加的每个数据都会高一些.那么,如何获得三个最新的东西并将它们显示在适当的地方呢? (对不起,我想不出如何表达我的问题)

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)

我没有喜欢html之类的东西,所以没有显示出想要的效果,但是我可以解释一下.我有一个具有4行的SQL表. ID,TITLE,DATE和POST.这是新闻提要,我想在首页上显示3条最新新闻文章".但是我不想只将它们显示为列表,我想将标题放在日期旁边的顶部div中,我想通过主键(ID)对其进行排序,然后将该帖子放在div中的下面标题和日期.这是尝试向您显示的内容:

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.

推荐答案

首先,请不要使用"date"作为字段名称,可以说您将其重命名为news_date.怎么样?

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++;
    }
?>

这篇关于在SQL表中选择最新数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆