用于根据字段确定记录优先级的逻辑


Logic for prioritizing records based on a field

我有一个表格,其中有消息列表,这些消息列表将随机显示在前端。消息表最近添加了一个名为"优先级"的字段,我在其中存储了 3 个值"低"、"中"、"高"。基于此优先级,记录的显示数量发生在前端。它在前端显示的次数也存储在字段名称"display"下的同一表中。是否可以在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>';
            }
        }

优先考虑所有可用消息,并在前端平等地显示它。现在我已经将数据库更改为另一个称为"优先级"的字段。

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

根据优先级,消息应该在前端提供。因此,查询必须执行基于此"优先级"和"显示"字段获取记录的逻辑。

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

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

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

也许是这样的

select * from c_messages order by priority, displayed desc;

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