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条)

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state