• Msyql Field函数讲解【转载】


    此函数比较简单,大家看看作为了解即可:

    MySQL可以通过field()函数自定义排序,格式:field(value,str1,str2,str3,str4),value与str1、str2、str3、str4比较,返回1、2、3、4,如遇到null或者不在列表中的数据则返回0. 这个函数好像Oracle中没有专门提供(也可能是我没有用到),不过自己实现这样一个函数还是比较简单的。

    mysql> select * from driver_log order by name;

    +--------+-------+------------+-------+

    | rec_id | name  | trav_date  | miles |

    +--------+-------+------------+-------+

    |      1 | Ben   | 2006-08-30 |   152 |

    |      9 | Ben   | 2006-09-02 |    79 |

    |      5 | Ben   | 2006-08-29 |   131 |

    |      8 | Henry | 2006-09-01 |   197 |

    |      6 | Henry | 2006-08-26 |   115 |

    |      4 | Henry | 2006-08-27 |    96 |

    |      3 | Henry | 2006-08-29 |   300 |

    |     10 | Henry | 2006-08-30 |   203 |

    |      7 | Suzi  | 2006-09-02 |   502 |

    |      2 | Suzi  | 2006-08-29 |   391 |

    +--------+-------+------------+-------+

    10 rows in set (0.00 sec)

    mysql> select * from driver_log order by field(name,'Suzi','Ben','Henry');

    +--------+-------+------------+-------+

    | rec_id | name  | trav_date  | miles |

    +--------+-------+------------+-------+

    |      2 | Suzi  | 2006-08-29 |   391 |

    |      7 | Suzi  | 2006-09-02 |   502 |

    |      1 | Ben   | 2006-08-30 |   152 |

    |      9 | Ben   | 2006-09-02 |    79 |

    |      5 | Ben   | 2006-08-29 |   131 |

    |      8 | Henry | 2006-09-01 |   197 |

    |      6 | Henry | 2006-08-26 |   115 |

    |      4 | Henry | 2006-08-27 |    96 |

    |      3 | Henry | 2006-08-29 |   300 |

    |     10 | Henry | 2006-08-30 |   203 |

    +--------+-------+------------+-------+

    10 rows in set (0.00 sec)

    mysql> select * from driver_log order by field(name,'Suzi','Ben');

    +--------+-------+------------+-------+

    | rec_id | name  | trav_date  | miles |

    +--------+-------+------------+-------+

    |     10 | Henry | 2006-08-30 |   203 |

    |      8 | Henry | 2006-09-01 |   197 |

    |      6 | Henry | 2006-08-26 |   115 |

    |      4 | Henry | 2006-08-27 |    96 |

    |      3 | Henry | 2006-08-29 |   300 |

    |      7 | Suzi  | 2006-09-02 |   502 |

    |      2 | Suzi  | 2006-08-29 |   391 |

    |      5 | Ben   | 2006-08-29 |   131 |

    |      9 | Ben   | 2006-09-02 |    79 |

    |      1 | Ben   | 2006-08-30 |   152 |

    +--------+-------+------------+-------+

    10 rows in set (0.00 sec)

    mysql> select * from driver_log order by field(name,'Suzi','Ben') desc;

    +--------+-------+------------+-------+

    | rec_id | name  | trav_date  | miles |

    +--------+-------+------------+-------+

    |      1 | Ben   | 2006-08-30 |   152 |

    |      9 | Ben   | 2006-09-02 |    79 |

    |      5 | Ben   | 2006-08-29 |   131 |

    |      2 | Suzi  | 2006-08-29 |   391 |

    |      7 | Suzi  | 2006-09-02 |   502 |

    |      8 | Henry | 2006-09-01 |   197 |

    |      6 | Henry | 2006-08-26 |   115 |

    |      4 | Henry | 2006-08-27 |    96 |

    |      3 | Henry | 2006-08-29 |   300 |

    |     10 | Henry | 2006-08-30 |   203 |

    +--------+-------+------------+-------+

    10 rows in set (0.00 sec)

    转载于:http://blog.csdn.net/lxz3000/article/details/6173873

    参考资料:https://segmentfault.com/a/1190000003742537

  • 相关阅读:
    HDU 4819 Mosaic --二维线段树(树套树)
    Codeforces Round #274 Div.1 C Riding in a Lift --DP
    ZOJ 3829 Known Notation --贪心+找规律
    JAVA成员变量和静态变量的区别
    spring注解
    js中double类型的数据加减的时候出错
    js在页面间传值的方法记录
    将表中数据转换成java entity实例
    最近都写APP的接口,有苦说不出啊.
    写于2018年第一场雪----记我的第一年工作
  • 原文地址:https://www.cnblogs.com/bing-yu12/p/7545741.html
Copyright © 2020-2023  润新知