按关键字对列进行排序
我有一个充满记录的数据库,通过使用这个 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 Rabbit
should 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.php
file.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过向名为 DisplayOrder 的表添加另一列,然后按它对表进行排序来完成此操作。
You can do this by adding another column to the table named DisplayOrder and then Order the table by it.
这可能会帮助您:
This may help you:
首先,您没有对发布的代码片段中的数据进行排序。如果您想要反向字母顺序,只需使用:
编辑:这也可能值得您花时间。 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:
EDIT: This may be worth your while, too. http://imthi.com/blog/programming/mysql-order-by-field-custom-field-sorting.php
这是一个有点开销,但它应该有效。
如果您有一个包含关键字和数字优先级的单独表,则可以在查询中向该表添加联接,然后按优先级列进行排序。
如果您只有十个固定关键字,您可能需要按案例使用顺序,就像其他受访者建议的那样。如果您有数十个关键字,或者一个动态更新或以任何显着频率更新的列表,您可能需要考虑优先级查找表。
此外,如果关键字有很多重复,您可能需要考虑将它们规范化到自己的表中,并将优先级作为字段。
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.