search

Home  >  Q&A  >  body text

mysql - 如何用sql语句,判断数据库中某字段的内容,包含于某字符串?

如何用sql语句,判断数据库中某字段的内容,包含于某字符串?也就是属于某字符串的子串?
比如数据库中某字段内容为“张三”,某字符串是“张三是好人”,则符合条件,
如果数据库中某字段内容为“张三”,某字符串是“张”,则不符合条件,

高洛峰高洛峰2787 days ago880

reply all(5)I'll reply

  • 黄舟

    黄舟2017-04-17 11:08:11

    select * from table t where t.a like '%'||t.b||'%';
    select * from table t where t.a like '%'||var||'%'

    (PL/SQL, fields a and b must be of string type, var is a string variable in the program, or more precise matching can use regular expressions, but the method is the same through string concatenation)

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 11:08:11

    where "%张三% like column_name

    sry The above is written backwards, it should be like this. If it is not mysql, the splicing method may be different.

    where '张三' like concat('%', column_name, '%')

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 11:08:11

    SELECT *
    FROM aaa
    where instr('$var',aaa.city)

    I found this from the Internet and found it useful.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 11:08:11

     select * from [表名] where [字段] like '%张三%';

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 11:08:11

    Use a concat function. You can turn the variable you want to match into something similar to a regular expression. For example: there are two fields in the user table, one is called username and the other is password. If you first want Find the line containing username in password, you can write it like this:

    select username,password from user where password like concat('%',username,'%');

    reply
    0
  • Cancelreply