zoukankan      html  css  js  c++  java
  • mysql判断是否包含某个字符的方法

    mysql判断是否包含某个字符的方法
    用locate 是最快的,like 最慢。position一般
    实战例子:
    select * from historydata
    where locate('0',opennum) and locate('1',opennum)
    order by number desc limit 10;

    方法一:locate(字符,字段名)
    使用locate(字符,字段名)函数,如果包含,返回>0的数,否则返回0 ,

    它的别名是 position in
    select * from 表名 where locate(字符,字段)
    select * from 表名 where position(字符 in 字段);

    例子:判断site表中的url是否包含'http://'子串,如果不包含则拼接在url字符串开头
    update site set url =concat('http://',url) where locate('http://',url)=0

    注意mysql中字符串的拼接不能使用加号+,用concat函数

    方法二:like
    SELECT * FROM 表名 WHERE 字段名 like "%字符%";

    方法三:find_in_set()
    利用mysql 字符串函数 find_in_set();

    SELECT * FROM users WHERE find_in_set('字符', 字段名);
    mysql有很多字符串函数 find_in_set(str1,str2)函数是返回str2中str1所在的位置索引,str2必须以","分割开。
    注:当str2为NO1:“3,6,13,24,33,36”,NO2:“13,33,36,39”时,判断两个数据中str2字段是否包含‘3’

    mysql > SELECT find_in_set()('3','3,6,13,24,33,36') as test;
    -> 1
    mysql > SELECT find_in_set()('3','13,33,36,39') as test;
    -> 0

    方法四:INSTR(字段,字符)
    select * from 表名 where INSTR(字段,字符)

  • 相关阅读:
    BZOJ3036: 绿豆蛙的归宿
    BZOJ1419: Red is good
    BZOJ1013: [JSOI2008]球形空间产生器sphere
    BZOJ1415: [Noi2005]聪聪和可可
    BZOJ1417: Pku3156 Interconnect
    BZOJ1076: [SCOI2008]奖励关
    BZOJ2318: Spoj4060 game with probability Problem
    BZOJ1426: 收集邮票
    BZOJ2720: [Violet 5]列队春游
    BZOJ2698染色
  • 原文地址:https://www.cnblogs.com/zdz8207/p/mysql-ocate-like-position.html
Copyright © 2011-2022 走看看