ITPub博客

首页 > 数据库 > MySQL > 【Mysql】instr与find_in_set与regexp

【Mysql】instr与find_in_set与regexp

原创 MySQL 作者:小亮520cl 时间:2016-08-25 15:57:30 0 删除 编辑
MySQL [interface_hd_com]> select * from tbl_name;
+---------------+
| dg_order_time |
+---------------+
| 1             |
| 1,2,3,4       |
| 2,3,4         |
| 1             |
| 2             |
+---------------+
5 rows in set (0.00 sec)

instr显示要查找的子串的位子

insrt:用来显示所在的坐标

  1. MySQL [interface_hd_com]> select instr(dg_order_time,'1') from tbl_name;
  2. +--------------------------+
  3. | instr(dg_order_time,'1') |
  4. +--------------------------+
  5. | 1 |
  6. | 1 |
  7. | 0 |
  8. | 1 |
  9. | 0 |
  10. +--------------------------+
  11. 5 rows in set (0.00 sec)
查找不包含1的
  1. MySQL [interface_hd_com]> select  * from tbl_name  where instr(dg_order_time,'1')=0;
    +---------------+
    | dg_order_time |
    +---------------+
    | 2,3,4         |
    | 2             |
    +---------------+
    2 rows in set (0.00 sec)

find_in_set:查找包含的

  1. MySQL [interface_hd_com]> select * from tbl_name where find_in_set('1',dg_order_time);
  2. +---------------+
  3. | dg_order_time |
  4. +---------------+
  5. | 1 |
  6. | 1,2,3,4 |
  7. | 1 |
  8. +---------------+
  9. 3 rows in set (0.00 sec)

  10. MySQL [interface_hd_com]> select * from tbl_name where find_in_set('2',dg_order_time);
  11. +---------------+
  12. | dg_order_time |
  13. +---------------+
  14. | 1,2,3,4 |
  15. | 2,3,4 |
  16. | 2 |
  17. +---------------+
  18. 3 rows in set (0.00 sec)

  19. MySQL [interface_hd_com]> select * from tbl_name where find_in_set('3',dg_order_time);
  20. +---------------+
  21. | dg_order_time |
  22. +---------------+
  23. | 1,2,3,4 |
  24. | 2,3,4 |
  25. +---------------+
  26. 2 rows in set (0.00 sec)

  27. MySQL [interface_hd_com]> select * from tbl_name where find_in_set('1,2',dg_order_time);
  28. Empty set (0.00 sec)
不能用来查找不包含的,并且只能查一个字符串

regexp正则表达式
  1. 基本语法:
  2. Regular Expression Operators



    1. 扩展正则表达式的一些字符是:
    2. ‘.’匹配任何单个的字符。
    3. 字符类“[...]”匹配在方括号内的任何字符。例如,“[abc]”匹配“a”、“b”或“c”。为了命名字符的范围,使用一个“-”。“[a-z]”匹配任何字母,而“[0-9]”匹配任何数字。
    4. “ * ”匹配零个或多个在它前面的字符。例如,“x*”匹配任何数量的“x”字符,“[0-9]*”匹配任何数量的数字,而“.*”匹配任何数量的任何字符。
    5. 如果REGEXP模式与被测试值的任何地方匹配,模式就匹配(这不同于LIKE模式匹配,只有与整个值匹配,模式才匹配)。
    6. 为了定位一个模式以便它必须匹配被测试值的开始或结尾,在模式开始处使用“^”或在模式的结尾用“$”。


  3. 简单用法
  4. MySQL [interface_hd_com]> select * from testtab;
    +---------+
    | id      |
    +---------+
    | 1       |
    | 1,2     |
    | 1,2,3,4 |
    | 1,2,3   |
    | 3,4,5   |
    +---------+
    5 rows in set (0.00 sec)


    MySQL [interface_hd_com]> SELECT * FROM testtab where id regexp '1|2|3|4';  ###包含1,2,3,4字符串的
    +---------+
    | id      |
    +---------+
    | 1       |
    | 1,2     |
    | 1,2,3,4 |
    | 1,2,3   |
    | 3,4,5   |
    +---------+
    5 rows in set (0.00 sec)


参考:
http://dev.mysql.com/doc/refman/5.6/en/regexp.html
http://www.111cn.net/database/mysql/55484.htm


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29096438/viewspace-2124033/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
毕业以后专业任职数据库工程师职位,itpub一直作为自己的笔记记录的地方,blog写的不详细,草书笔记,仅供参考!

注册时间:2013-09-12

  • 博文量
    529
  • 访问量
    1126950