douxiduan8344 2012-03-27 19:02
浏览 19
已采纳

MySQL / PHP按多列排序,顶部有一列

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:

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.

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. 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.

  • 写回答

2条回答 默认 最新

  • doupin5667 2012-03-27 19:28
    关注

    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. 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';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题