php - 尝试解决有关查询中 ORDER BY 的问题

标签 php mysql

我创建了一个 HTML 表,并根据查询进行了填充,并将查询设置为按降序排序,但是查询运行并且结果显示在表我无法获取运行日期降序

数据库已正确设置为名为 walk_date 的日期字段。我还使用 strtotime 将日期排序为人类友好的。

05.10.2012
07.10.2012
18.08.2012
21.10.2012
22.09.2012

这是他们出来的顺序,下面是我的代码:

<h1>A Photographic Diary</h1>
    <p>&nbsp;</p>

<div id="about-text">

<p>All walks are listed in order with the latest at the top. Walks can be filtered by area using the drop-down box. The abbreviated Area ID's are as follows:<br/>
YD  -   Yorkshire Dales<br/>
LD  -   Lake District<br/>
FB  -   Forest of Bowland<br/>
WPM -   West Pennine Moors<br/>
PD  -   Peak District<br/>
LOC -   Local<br/>
O   -   Other<br/></p>
</div>
 <!--Selects walk area -->
<br /> <form id="form1" name="form1" method="post" action="walk_results.php">
<label>Area</label>
<select name="area_id">
<option value="">All</option>


<?php
$sql_result = mysql_query("SELECT * FROM walk GROUP BY area_id ORDER BY walk_date DESC");
while ($row = mysql_fetch_assoc($sql_result)) {
        echo "<option value='".$row["area_id"]."'".($row["area_id"]==$_REQUEST["area_id"]? " selected" : "").">".$row["area_id"]."</option>";
};

?>
<!-- Submits walk area -->
</select>
  </label>

  <input type="submit" name="button" id="button" class="styled-button" value="Select Area" /></form> 
   <style type="text/css">
   .styled-button {
    -webkit-box-shadow:rgba(0,0,0,0.2) 0 1px 0 0;
    -moz-box-shadow:rgba(0,0,0,0.2) 0 1px 0 0;
    box-shadow:rgba(0,0,0,0.2) 0 1px 0 0;
    border-bottom-color:#333;
    border:1px solid #6470ED;
    background-color:#6470ED;
    border-radius:3px;
    -moz-border-radius:5px;
    -webkit-border-radius:5px;
    color:#fff;
    font-family:Verdana, Arial, Helvetica, sans-serif;
    font-size:12px;
    text-shadow:#b2e2f5 0 1px 0;
    padding:2px
}


  </style>
</select>
  </label>  

<!-- Walk area results table -->
<div id= "result">
<br /><br />
<table width="900" border="1" cellspacing="1" cellpadding="4">
  <tr>
    <td width="84" bgcolor="#6470ED" align="center"><strong>Select Walk</strong></td>
    <td width="75" bgcolor="#6470ED" align="center"><strong>Walk Date</strong></td>
    <td width="360" bgcolor="#6470ED" align="center"><strong>Title</strong></td>
    <td width="64" bgcolor="#6470ED" align="center"><strong>Distance (Miles)</strong></td>
    <td width="85" bgcolor="#6470ED" align="center"><strong>Ascent</strong></td>
    <td width="78" bgcolor="#6470ED" align="center"><strong>Time</strong></td>
    <td width="67" bgcolor="#6470ED" align="center"><strong>Area Id</strong></td>
  </tr>


<?php

if ($_REQUEST["area_id"]<>'') {
    $search_area_id = "AND area_id='".mysql_real_escape_string($_REQUEST["area_id"])."'";
}

if ($_REQUEST["from"]<>'' and $_REQUEST["to"]<>''){

}

else {
    $sql = "SELECT * FROM walk  WHERE walk_id>\"\"".$search_string.$search_area_id ;

}

$sql_result= mysql_query ($sql) or die ('request"Error"'.$sql);
#echo "SQL $sql";  --- to view info ---

$clicked =$_REQUEST["area_id"];
$sql= "SELECT * FROM walk  WHERE area_id = '$clicked'";

if (mysql_num_rows($sql_result)>0) {

    while ($row = mysql_fetch_assoc($sql_result)) {
    $id = $row['walk_id'];
    $date_of_walk = $row['walk_date']; 
    $date_of_walk = date("d.m.Y", strtotime($date_of_walk));
#echo "id $id<br>";  --- to view info ---


?>  
<!-- Table Results -->
  <tr>
    <td><?php echo "<a href='walk_info.php?walk_id=$id'>Select Walk"?></a></td>
    <td><?php echo $row['walk_date']=$date_of_walk;?></td>
    <td><?php echo $row["title"]; ?></td>
    <td><?php echo $row["distance"]; ?></td>
    <td><?php echo $row["ascent"]; ?></td>
    <td><?php echo $row["time"]; ?></td>
    <td><?php echo $row["area_id"]; ?></td>
  </tr>

<?php
    }
} else {  

$row= mysql_fetch_assoc($sql_result);$sql_result= mysql_query ($sql);
?>
<tr><td colspan="5">No results found.</td>
<?php   
 }
mysql_close()?>

最佳答案

如果您将日期存储为标准的 MySQL 日期格式 (YYYY-MM-DD),这将很容易用 SQL 完成。只需在带有 walk_date 字段的 ORDER BY 子句中使用 DESC 关键字:

   SELECT *
     FROM table_name
 ORDER BY walk_date DESC

关于php - 尝试解决有关查询中 ORDER BY 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14288602/

相关文章:

php - Laravel 5.1 - 在保存到数据库时生成唯一的 10 个字母数字字符代码

php - 如何将 Python 卖给客户/老板/个人

PHP 无法计算 4 个数据输出

java - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException : Communications link failure

php - 为什么我的服务器将添加值视为mysql_query中的identifier_link?

mysql - VB.NET 2010 和 MySql - 为懒惰的人处理 DB NULL

php - 过滤输入和事件

php - Laravel 分页器显示更多页面

输入为 1970 的 PHP 日期

java - 将数据库中的日期与我从java jsp和sql中的inputtext获得的值进行比较