简体   繁体   English

在不同站点上以不同顺序对MySQL字段进行排序

[英]Sorting MySQL fields in different order on different sites

Let's say I have a table called "Fruits" and in that table I have a row called "Fruit" which has six entries: Apple, Banana, Kiwi, Orange, Peach, Tangerine. 假设我有一个名为“水果”的表,并且在该表中有一个名为“水果”的行,其中有六个条目:苹果,香蕉,猕猴桃,橙子,桃子,橘子。

I have four websites that want to sort the result of SELECT * FROM Fruits in different ways, but there is no pattern to them. 我有四个网站希望以不同的方式对SELECT * FROM Fruits的结果进行排序,但是它们没有模式。 They're not random , since each site will decide a different way to sort them. 它们不是随机的 ,因为每个站点都会决定对它们进行排序的不同方法。 For example, one site might be "Kiwi, Apple, Banana, Tangerine, Orange, Peach" and the next might be, "Banana, Kiwi, Apple, Peach, Tangerine, Orange." 例如,一个站点可能是“猕猴桃,苹果,香蕉,橘子,橘子,桃子”,下一个站点可能是“香蕉,猕猴桃,苹果,桃子,橘子,橘子”。

Can these be sorted in MySQL like this, bearing in mind that future items might be added and need to be sorted again arbitrarily at the whim of each site? 是否可以在MySQL中像这样对它们进行排序,同时牢记将来的项目可能会添加,并且需要在每个站点的心血来潮时任意重新排序? Or is there a way to sort these results in PHP after they have been pulled? 还是有一种方法可以将这些结果在PHP中排序后进行排序?

One possible approach is setting up so-called 'sorting maps' - strings of fruit names, having the items in the exact order you want to them to be shown. 一种可能的方法是设置所谓的“排序图”(sorting maps)-一串水果名称,并按照您希望它们显示的确切顺序排列它们。 Then you'll be able to use this array with FIND_IN_SET MySQL function: 然后,您将可以将此数组与FIND_IN_SET MySQL函数一起使用:

$sortMap = 'Kiwi,Apple,Banana,Tangerine,Orange,Peach';

$query = >>>QUERY
   SELECT fruit_id, fruit_name
     FROM fruits
 ORDER BY FIND_IN_SET(fruit_name, '$sortMap');
QUERY;

// do something with the query

To change the ordering, just set the $sortMap on per-site basis. 要更改顺序,只需在每个站点上设置$sortMap

Ok, this ended up working the best here: 好的,这最终在这里效果最佳:

    SELECT `fruit` 
    FROM (`Fruit`) 
    ORDER BY FIELD(`fruit`, 'Tangerine','Banana','Apple', 'Orange', 'Peach', 'Kiwi') ASC

As raina77ow suggested, I created a sort map to hold the values I want to inject into the query. 正如raina77ow所建议的那样,我创建了一个排序映射,以保存要注入查询中的值。 That gets held as a config value we can switch for each site. 这被保存为一个配置值,我们可以为每个站点切换。 Thanks for the direction! 感谢您的指导!

Try this : 尝试这个 :

 SELECT * FROM Fruits ORDER BY rand()
  • this will give you a random result set order ! 这将给您随机的结果集顺序!

You would need to assign some sort of site order identifier to each value, or at least logically group them with a fruit_type field. 您需要为每个值分配某种站点订单标识符,或者至少在逻辑上将它们与fruit_type字段进行分组。 Otherwise you can use 否则你可以使用

SELECT ... IF(fruit = 'apple',1,0) as fruit_apple ... ORDER BY ... fruit_apple

It's an extremely inelegant solution. 这是一个非常优雅的解决方案。

Have you considered normalising the tables, moving the apples & ordering to a separate table and then joining them on request? 您是否考虑过将表格标准化,将苹果和订单移至另一个表格,然后根据要求将它们加入?

使用此ORDER BY rand()检索值;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM