0

曾试图在别处寻找答案,但没有成功。任何想纠正我的人,我确信我错过了一些基本的东西——我只是找不到什么。

我有 11 列的表“数据”,包括“from_Date”和“to_date”。当前显示的结果首先显示最远的日期。我要求结果首先显示为最接近的日期(即ASC/DESC)。

已尝试ORDER BYfrom_date - 无效。我的搜索表单代码中的示例数据/页面上的结果(供参考)

<form action="webdeals.php" method="post" name="form1" id="form1" style="margin-bottom: 0;">
  <table width="839" border="0">
    <tr valign="top">
      <td width="83">&nbsp;</td>
      <td width="105"><span class="style31">Course Type:</span>
      </td>
      <td width="161">
        <select name="city">
          <option value="">--</option>
          <?php $sql="SELECT * FROM " .$SETTINGS[ "data_table"].
          " GROUP BY city ORDER BY city"; $sql_result=m ysql_query ($sql, $connection ) or die (
          'request "Could not execute SQL query" '.$sql); while ($row=m ysql_fetch_assoc($sql_result)) { echo
          "<option value='".$row[ "city"]. "'
    ".($row[ "city"]==$_REQUEST[ "city"] ? "selected" : ""). ">".$row[
          "city"]. "</option>"; } ?>
        </select>
      </td>
      <td width="100" class="style27"><span class="style31">From:</span>
      </td>
      <td width="108">
        <input name="from" type="text" id="from" size="10" value="
    <?php echo $_REQUEST   [" from "]; ?>" />
      </td>
      <td width="104">
        <img src="images/calendar_icon_pack.png" width="32" height="32" />
      </td>
      <td width="153">
        <div align="center">
          <input type="image" src="http://www.scotsail.co.uk/images/filter_button.png"
          alt="Refine Results" />
        </div>
      </td>
    </tr>
    <tr valign="top">
      <td class="style23">&nbsp;</td>
      <td class="style31">Price Search:</td>
      <td>
        <input type="text" name="string" id="string" value="
    <?php echo stripcslashes($_REQUEST[" string "]); ?>" />
      </td>
      <td>
        <label for="to2" class="style31">To:</label>
      </td>
      <td>
        <input name="to" type="text" id="to" size="10" value="<?php echo $_REQUEST["
        to "]; ?>"/>
      </td>
      <td>
        <img src="images/calendar_icon_pack.png" width="32" height="32" />
      </td>
      <td>
        <div align="center"><a href="webdeals.php">Reset</a>
        </div>
      </td>
    </tr>
  </table>
  <label for="from" class="style2"></label>
</form>

然后:

<?php 

if ($_REQUEST[ "string"]<>'') {
    $search_string = " AND (full_name LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR email LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%')";
}


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


if ($_REQUEST["from"]  <>'' and $_REQUEST["to"]    <>'') {
    $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE from_date
  >= ' ".mysql_real_escape_string($_REQUEST["from"])."' AND to_date
  <='".mysql_real_escape_string     ($_REQUEST["to"])."' ".$search_string.$search_city;
} else
if ($_REQUEST["from "]<>'') {
    $sql = "SELECT * FROM ".$SETTINGS["data_table "]." WHERE from_date>= ' ".mysql_real_escape_string ($_REQUEST["from"])."'".$search_string.$search_city;
} else
if ($_REQUEST["to"]        <>'') {
    $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE to_date
      <='".mysql_real_escape_string    ($_REQUEST["to"])."' ".$search_string.$search_city;
} else {
    $sql = "SELECT * FROM ".$SETTINGS["data_table "]." WHERE id>0".$search_string.$search_city;
}

$sql_result = mysql_query ($sql, $connection            ) or die ('request "Could not execute SQL query" '.$sql);

if (mysql_num_rows($sql_result)>0)            {
    while ($row = mysql_fetch_assoc($sql_result)) {
        ?>

如果这是一个基本问题,我们深表歉意,但将头发拉出来无法使其正常工作。


更新:感谢@Veger 和其他人的评论。

} else { 
    $sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0 ORDER BY from_date ".$search_string.$search_city;
}

确实会导致“未优化”结果按 from_date 排序,但这会破坏对“城市”列/字段的搜索,并且在调用 from_date 和 to_date 时不起作用。关于它的外观有什么想法吗?

4

1 回答 1

0

您在语句$string_string之后添加了您的内容ORDER BY,这会导致 SQL 查询不正确:

SELECT * FROM <table> 
  WHERE id>0 
  ORDER BY from_date AND (full_name LIKE '%<fullname>%')

您需要交换两者:

$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE id>0 ".$search_string.$search_city." ORDER BY from_date";

所以你的查询变成这样:

    SELECT * FROM <table> 
      WHERE id>0 AND (full_name LIKE '%<fullname>%')
      ORDER BY from_date

有关ORDER BY语法和语句顺序的更多信息,请查看mysqlORDER BY文档

于 2013-01-13T16:58:52.693 回答