简体   繁体   English

MySQL / PHP按多列排序,其中一列在顶部

[英]MySQL/PHP Order By Multiple Columns with one at the top

The title might not explain my problem fully but I wasn't sure how to in a title so I will explain more here. 标题可能无法完全解释我的问题,但是我不确定如何在标题中使用,因此我将在此处进行更多说明。

I have a table which holds various information on bands/acts and I have a browse page which shows all the acts in a certain area etc. Each act can say whether they want drinks/food/accommodation/extra/travel to be paid for. 我有一张桌子,上面保存着乐队/表演的各种信息,我有一个浏览页面,显示某个地区等的所有表演。每个表演都可以说出他们是否要支付饮料/食物/住宿/额外/旅行的费用。 On the browse page I also have a sort function which I want to be able to sort the results by which acts want what paid. 在浏览页面上,我还具有一个排序功能,我希望能够对要付款的行为进行排序。

There are five check boxes for each of the options but when I check only travel for example, acts which have travel as well as the others ticked are on top when I want acts which only want travel to be paid for on top. 每个选项都有五个复选框,但是例如,当我只选择旅行时,当我希望仅向旅行支付费用的行为时,具有旅行以及其他打钩的行为位于顶部。

Currently the sort function starts with the original query from when browsing: 当前,排序功能从浏览时的原始查询开始:

SELECT * FROM acts

And then the sort function appends the ORDER BY statement depending on what has been checked: 然后,sort函数根据已检查的内容追加ORDER BY语句:

SELECT * FROM acts ORDER BY travel DESC

Or if more than one was selected: 或者,如果选择了多个:

SELECT * FROM acts ORDER BY travel DESC, food DESC

But as I said this doesn't order the list properly (so for the second order by query I'd want an act which only wanted travel and food at the top). 但是,正如我所说的那样,该列表的排序不正确(因此对于查询的第二个订单,我想要一个只需要旅行和食物的行为)。

I've also tried adding in ORDER BY ASC for the options which weren't selected but this didn't help. 我还尝试为未选择的选项添加ORDER BY ASC,但这无济于事。

Also the columns in the database hold either "yes" or "no" which is why I've used DESC when I want them to be on top. 此外,数据库中的列包含“是”或“否”,这就是为什么当我希望将它们放在首位时使用DESC的原因。 I didn't make the database and it can't be changed either. 我没有建立数据库,也无法更改。

Thanks for any help. 谢谢你的帮助。

EDIT: you can see what I mean here: http://www.acthook.com/browse.php If you leave the browse boxes blank and click browse to get all results, then tick travel and click sort, there is an act wanting only travel part way down the first page. 编辑:您可以在这里看到我的意思: http : //www.acthook.com/browse.php如果将浏览框保留为空白,然后单击浏览以获取所有结果,然后勾选旅行并单击排序,则有一种需要仅沿第一页向下移动。

I'm not sure if it's right idea (problem is in a database structure), but try something like this> 我不确定这是否是正确的主意(问题在数据库结构中),但是尝试这样的方法>

check only travel: SELECT * FROM table WHERE travel = yes and food = no and .. = no UNION SELECT * FROM table 仅检查旅行:SELECT * FROM table WHERE travel = yes ,食物= no ,.. = no UNION SELECT * FROM table

EDIT: or maybe something like SELECT * FROM order by travel, food DESC 编辑:或类似选择*从旅行,食物DESC顺序

Oh I see, thanks for the example, it wasn't that clear without :) Then you should have something like this: 哦,我明白了,感谢您的示例,没有:)还不清楚,然后您应该有类似以下内容:

SELECT * FROM acts ORDER BY travel DESC, food ASC, accomodation ASC, drink ASC, extra_fee ASC;

And if the user clicks on food too: 如果用户也点击了食物:

SELECT * FROM acts ORDER BY travel DESC, food ASC, accomodation ASC, drink ASC, extra_fee ASC;

Or if he clicks only on food: 或者,如果他只点击食物:

SELECT * FROM acts ORDER BY food DESC, travel ASC, accomodation ASC, drink ASC, extra_fee ASC;

As you can see here, the order of the fields in the order clause have been changed. 如您所见,order子句中字段的顺序已更改。 You'll have to pay attention to that too. 您也必须注意这一点。

In order to build the query, you can do something like this : 为了构建查询,您可以执行以下操作:

$allowedOrderBy = array('travel','food','dring','extra_fee');//etc
$foundOrderBy = array();
foreach ($_POST as $orderBy) {
    if (in_array($orderBy, $allowedOrderBy)) {
         $foundOrderBy[] = $orderBy;
    }
}
$notFoundOrderBy = array_diff($allowedOrderBy, $foundOrderBy);
$orderByStr = implode(' DESC,', $foundOrderBy).' DESC, ';
$orderByStr.= implode(' ASC,', $notFoundOrderBy). 'ASC';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM