douwen5584 2016-07-19 03:55
浏览 36
已采纳

然后计算PHP和MySQL中的排序函数

I have this database wherein it has columns named Mall (it contains different names of Malls), NameOfStore (it contains name of stores) and AMEX (it contains either YES or NO depending if the store accepts American Express or not).

What I want to do is to count the number of stores accepting AMEX for each mall and then sort the malls in a descending manner according to their number of stores accepting AMEX. I want to incorporate a "Sorting" feature in a dropdown list form where users of my program can choose if they want to sort the malls with the most AMEX, etc. I was able to make a dropdown list for my "filter according to some condition" feature but this Sorting feature is much different.

I'm not sure on how I'm going to merge and properly sequence the logic for these queries.

 function sortStore($conn, $sort)
    {
    $table = "table1";
    $column1 = "Mall";
    $column2 = "NameOfStore";
    $condition = "AMEX = 'YES'"

    $query = Select Count($column2) from $table where $condition AND $sort";
    ..other codes..
    //$sort would contain cases (one case for each mall)
    //Example: case "Mall1": $sort = "Mall = 'Mall1'; break;
    //I'm actually not sure if the logic on my query is correct based on what I want to happen
    }

Then I would want to ORDER the Malls by the Number of Stores they have that are accepting AMEX in a descending order. I need to use ORDER BY function but I'm not sure where to place it in this particular context.

SUMMARY:

Dropdown list

User chooses what category he wants the mall to be sorted (Example: Number of Stores with AMEX, Number of Restaurants, etc.)

Program displays ordered list of Malls for the chosen category

  • 写回答

2条回答 默认 最新

  • dongyou6795 2016-07-19 04:20
    关注

    This query should give you the desired result.

    select mall, count(AMEX) AS AMXCNT 
    from table1 
    WHERE AMEX='yes' 
    GROUP BY MALL ORDER BY AMXCNT
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题