简体   繁体   English

SELECT * FROM table_name ORDER BY column_name?

[英]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.

相关问题 Select * from table_name where column_nameِ = &#39;value&#39; - Select * from table_name where column_nameِ = 'value' SELECT id FROM table_name WHERE column = $variable - SELECT id FROM table_name WHERE column = $variable QueryException:常规错误:1没有这样的表:{table_name}(SQL:从“ {table_name}”中选择*) - QueryException: General error: 1 no such table: {table_name} (SQL: select * from “{table_name}”) 选择列 FROM table_name WHERE username=$_SESSION[&#39;username&#39;] 不起作用 - Select column FROM table_name WHERE username=$_SESSION['username'] not working SET @var:= SELECT * FROM table_name:是可能的 - SET @var := SELECT * FROM table_name: is it possible ORDER BY column_name帮助(通过HTML表视图中的链接)(PHP MySQL - ORDER BY column_name help (via link in HTML table view) (PHP MySQL 如何对列数进行ORDER BY column_name计数 - How to ORDER BY column_name counting the number of strings 在table_name内的列内编辑列? - Edit column inside column inside table_name? 如何在 laravel mongodb 中选择 COUNT(column_name) - How to SELECT COUNT(column_name) in laravel mongodb get_results(&quot;select * from table_name where id IN($array)&quot;) 但它假定 $array 为 &quot;Array&quot; - get_results("select * from table_name where id IN($array)") but it assumes $array as "Array"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM