mysql - 如何用sql语句,判断数据库中某字段的内容,包含于某字符串?
高洛峰
高洛峰 2017-04-17 11:06:11
0
5
884

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

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(5)
黄舟
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)

大家讲道理
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, '%')
左手右手慢动作

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

I found this from the Internet and found it useful.

伊谢尔伦
 select * from [表名] where [字段] like '%张三%';
洪涛

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,'%');
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template