douti9286 2011-08-26 00:39
浏览 86
已采纳

在PHP / MySQL中对符合特定条件的行进行排序

I have a database table with 6 fields that holds information about restaurants:
id
name
type
category_1
category_2
category_3

There are three "types" of restaurants: gold, silver, and bronze. Gold restaurants are featured on all 3 of their category pages (category_1, category_2, and category_3). Silver restaurants are featured on their top two categories (category_1 and category_2). Bronze restaurants are featured on their first category only (category_1). On my category pages, I need to display all restaurants in those categories, but Feature the restaurants who qualify to be featured (ie, they need to be listed at the top). I'm not sure how to do this with PHP / MySQL. I can query restaurants in the categories just fine, but how do I tell it to put Featured restaurants at the top? This is what I would need to do on the category "Mexican":

Display all mexican restaurants. Promote Gold restaurants that have "Mexican" listed in any of their category fields, Promote Silver restaurants that have "Mexican" in their top two category fields, and promote Bronze restaurants that have "Mexican" in it's top category field.

I've been thinking of many different ways to do this and can't figure it out.

  • 写回答

3条回答 默认 最新

  • dpp3047 2011-08-26 01:06
    关注

    This should help you out with your existing table structure.
    I'm assuming type contains bronze, silver, and gold in this query.

    SELECT * FROM restaurants 
    WHERE category_1 = 'Mexican' 
      OR (category_2 = 'Mexican' AND type in ('silver','gold'))
      OR (category_3 = 'Mexican' AND type = 'gold')
    ORDER BY Field(type,'gold','silver','bronze'), name asc;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗