按关键字对列进行排序

发布于 2024-11-07 21:38:23 字数 1099 浏览 0 评论 0原文

我有一个充满记录的数据库,通过使用这个 MySQL 语法:

 public static function allfood($db) {
    $sql = "SELECT DISTINCT sort, sortname FROM tblDishes WHERE foodanddrinks = 'e'";
    $result = $db->listing($sql);
    return $result;
  }

我得到了按字母顺序排列的所有结果。但我想确定我自己的顺序。例如,Rabbit 应显示在 Apple 之前。是否有 SQL 语法允许我组织自己的显示顺序?

编辑
这在我的 dbconnections.php 文件中进行了说明。

public function listing($sql) {
    $result = mysql_query($sql, $this->_connection);
    while($row=mysql_fetch_array($result)) {
      $return[] = $row;
    }
    return $return;
  }

这就是我尝试过的事情,但我在 dbconnections.php 文件中收到错误

public static function allfood($db) {
        $sql = "SELECT DISTINCT sort, sortname FROM tblDishes WHERE foodanddrinks = 'e' order by case sortname 
        when 'Rabbit' then smth1
        when 'Apple' then smth2
        when 'Fish' then smth3
        when 'Pasta' then smth4
        when 'Snacks' then smth5";
        $result = $db->listing($sql);
        return $result;
      }

I have a database filled with records and by using this MySQL-syntax:

 public static function allfood($db) {
    $sql = "SELECT DISTINCT sort, sortname FROM tblDishes WHERE foodanddrinks = 'e'";
    $result = $db->listing($sql);
    return $result;
  }

I get all the results ordered alphabetically. But I would like to determine my own order. For example Rabbitshould be displayed before Apple. Is there a SQL-syntax which allows me to organize my own order of display?

EDIT
This is stated in my dbconnections.phpfile.

public function listing($sql) {
    $result = mysql_query($sql, $this->_connection);
    while($row=mysql_fetch_array($result)) {
      $return[] = $row;
    }
    return $return;
  }

And this is the thing I tried, but I get an error in the dbconnections.php file

public static function allfood($db) {
        $sql = "SELECT DISTINCT sort, sortname FROM tblDishes WHERE foodanddrinks = 'e' order by case sortname 
        when 'Rabbit' then smth1
        when 'Apple' then smth2
        when 'Fish' then smth3
        when 'Pasta' then smth4
        when 'Snacks' then smth5";
        $result = $db->listing($sql);
        return $result;
      }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

贩梦商人 2024-11-14 21:38:23

您可以通过向名为 DisplayOrder 的表添加另一列,然后按它对表进行排序来完成此操作。

You can do this by adding another column to the table named DisplayOrder and then Order the table by it.

葬花如无物 2024-11-14 21:38:23

这可能会帮助您:

SELECT * FROM tblDishes WHERE foodanddrinks = 'e'
order by case field_name
when 'Rabbit' then smth1
when 'Apple' then smth2
...
else smth666 
end

This may help you:

SELECT * FROM tblDishes WHERE foodanddrinks = 'e'
order by case field_name
when 'Rabbit' then smth1
when 'Apple' then smth2
...
else smth666 
end
荒岛晴空 2024-11-14 21:38:23

首先,您没有对发布的代码片段中的数据进行排序。如果您想要反向字母顺序,只需使用:

public static function allfood($db) {
  $sql = "SELECT * FROM tblDishes WHERE foodanddrinks = 'e' ORDER BY foodanddrinks DESC";
  $result = $db->listing($sql);
  return $result;
}

编辑:这也可能值得您花时间。 http://imthi.com/blog/programming /mysql-order-by-field-custom-field-sorting.php

First of all, you are not sorting the data in the snippet you posted. If you want an inversed alphabetical order, simply use:

public static function allfood($db) {
  $sql = "SELECT * FROM tblDishes WHERE foodanddrinks = 'e' ORDER BY foodanddrinks DESC";
  $result = $db->listing($sql);
  return $result;
}

EDIT: This may be worth your while, too. http://imthi.com/blog/programming/mysql-order-by-field-custom-field-sorting.php

幸福不弃 2024-11-14 21:38:23

这是一个有点开销,但它应该有效。

如果您有一个包含关键字和数字优先级的单独表,则可以在查询中向该表添加联接,然后按优先级列进行排序。

如果您只有十个固定关键字,您可能需要按案例使用顺序,就像其他受访者建议的那样。如果您有数十个关键字,或者一个动态更新或以任何显着频率更新的列表,您可能需要考虑优先级查找表。

此外,如果关键字有很多重复,您可能需要考虑将它们规范化到自己的表中,并将优先级作为字段。

It's a bit of an overhead, but it should work.

If you have a separate table containing keywords and a numeric priority, you could add a join to that table in your query, and then order by the priority column.

If you've only got, say, ten fixed keywords, you'll probably want to use order by case, like other respondents have suggested. If you've got dozens of keywords, or a list that's updated dynamically or with any notable frequency, you might want to consider a priority lookup table.

Also if the keywords have a lot of repetition, you might want to think about normalising them out into their own table anyway, with priority as a field.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文