基于字段确定记录优先级的逻辑

时间:2015-01-12 03:12:31

标签: php mysql sql logic

我有一个表格,其中的消息列表将随机显示在前端。消息表有一个名为' priority'最近添加了我存储3个值' low'' medium'' high'。基于此优先级,记录的显示次数发生在前端。它在前端显示的次数也存储在字段名称'下显示的同一个表格中。在mysql查询中是否可以根据此优先级选择要显示的记录?列和'显示的次数'列有些怎么样?或者必须只在php级别完成?

 The older table format:
 ID    MESSAGEHEAD        MESSAGEDESC             DISPLAYED
 1     message title 1    message description 1   15
 2     message title 2    message description 2   17
 3     message title 3    message description 3   16
 and so on....

所以我编写了以下代码来获取要在前端显示的消息。

        // Logic of getting message based on the configuration.
        $messages_query = @mysql_query('select * from c_messages order by displayed asc limit 3');
        if(@mysql_num_rows($messages_query)){
            $total_messages = @mysql_num_rows($messages_query);
            while($message = @mysql_fetch_array($messages_query)){
                $messages_xml .= '<message><messagehead>'.$message['messagehead'].'</messagehead><mesagedesc>'.$message['mesagedesc'].'</mesagedesc></message>';
            }
        }

它优先考虑所有可用消息,并在前端同等显示。现在我已经将数据库更改为还有一个名为&#39; priority&#39;。

的字段
 The new table format:
 ID    MESSAGEHEAD        MESSAGEDESC             DISPLAYED    PRIORITY
 1     message title 1    message description 1   15           HIGH
 2     message title 2    message description 2   17           LOW
 3     message title 3    message description 3   16           MEDIUM
 and so on....

根据PRIORITY,消息应该在前端提供。因此,查询必须根据此优先级来执行获取记录的逻辑。并且&#39;显示&#39;字段。

我的意思是显示时间应该如下......

  MESSAGE        PRIORITY    DISPLAYED
  MESSAGE1     - HIGH      - 45 TIMES
  MESSAGE2     - HIGH      - 45 TIMES
  MESSAGE3     - LOW       - 15 TIMES
  MESSAGE4     - MEDIUM    - 30 TIMES

您在上面看到的优先级决定了消息显示的次数。真的很抱歉,我无法清楚地知道如何实现这一目标。如果有人有逻辑,请善解释。如果你可以提示我需要继续的方式,我可以试着让它解决。非常感谢提前。

1 个答案:

答案 0 :(得分:0)

也许就像这样

select * from c_messages order by priority, displayed desc;

如果您按增加优先级顺序声明枚举,则在优先级之后添加desc;