[英]SELECT * FROM table_name ORDER BY column_name?
ok so i coded in a news section and everytime i insert new news,its shows below the old one. 好的,所以我在新闻部分进行了编码,每次我插入新新闻时,它都会在旧新闻下面显示。 i want to make it ORDER BY id but make it start like backwards.i dont know how to explain but yeh i want them to be ordered by the newest added by the id so if the 1st row that was inserted's id is 1 then i want it to show below the next id.
我想按ID排序,但让它像向后一样开始。我不知道该怎么解释,但是我希望它们按ID所添加的最新顺序排序,所以如果插入的ID的第一行是1,那么我要它显示在下一个ID下方。
so row with id = 2 will be here 所以id = 2的行会在这里
so row with id = 1 will be here 所以id = 1的行会在这里
thats how i want it to be, instead of it being like this 那就是我想要的样子,而不是像这样
so row with id = 1 will be here 所以id = 1的行会在这里
so row with id = 2 will be here 所以id = 2的行会在这里
. 。
Sorry for my bad explanation i hope this is understandable 对不起,我的解释不好,我希望这是可以理解的
heres my code so far 到目前为止我的代码
<?php
require("include/config.php");
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id");
while($row = mysql_fetch_array($sqlnews)) {
$dbdate = $row['date'];
$dbnews = $row['news'];
echo "<h1><strong>$dbdate</strong></h1>";
echo "<div class='content'>$dbnews</div><br><br>";
}
?>
add DESC
in your ORDER BY
clause 在您的
ORDER BY
子句中添加DESC
SELECT * FROM news ORDER BY id DESC
by default, it is in ASC
mode. 默认情况下,它处于
ASC
模式。
SELECT * FROM news ORDER BY id DESC
DESC is the descending keyword DESC是降序关键字
ASC is ascending ASC正在上升
If you specify neither then default behaviour is ascending 如果您都不指定,则默认行为是递增的
只需在SQL查询中使用DESC
关键字即可。
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
However, it isn't really such a good idea to use id, because semantically, there is nothing preventing somebody from changing the sequence which counts up automatically assigning the ID. 但是,使用id并不是一个好主意,因为从语义上讲,没有什么可以阻止别人更改自动分配ID的顺序。
Therefore, you should add a column created_at
. 因此,您应该添加一列
created_at
。 Everytime you insert a row, you can use the SQL function NOW()
. 每次插入一行时,都可以使用SQL函数
NOW()
。
The advantage is that you can say: 好处是您可以说:
SELECT * FROM news WHERE created_at <= NOW() ORDER BY created_at DESC
This means that you can schedule news items ahead of time, and it will automatically display when the date/time arrives! 这意味着您可以提前安排新闻项目,并且它将在日期/时间到来时自动显示!
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
try this: 尝试这个:
just have to add 只需添加
order by id DESC
Just replace your code by this code: 只需用以下代码替换您的代码:
<?php
require("include/config.php");
$sqlnews = mysql_query("SELECT * FROM news ORDER BY id DESC");
while($row = mysql_fetch_array($sqlnews)) {
$dbdate = $row['date'];
$dbnews = $row['news'];
echo "<h1><strong>$dbdate</strong></h1>";
echo "<div class='content'>$dbnews</div><br><br>";
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.