繁体   English   中英

使用MySQL在字符串内查找确切的文本

[英]Find an exact text inside an string with MySQL

我有一个包含两列的数据库表,一列用于ID,另一列用于Colors。 假设我在此表中有3行,包含以下数据:

ID: 1 => Colors: Blue; Yellow; Red;
ID: 2 => Colors: Green; GreenYellow; Yellowgreen; Blue;
ID: 3 => Colors: Yellow; Blue;

如果我想进行SQL调用以了解是否有黄色的寄存器(仅黄色,没有GreenYellow或Yellowgreen)。 我该怎么做? 我正在尝试:

$statement = $conexion->prepare("SELECT * FROM comentarios  WHERE suscritos LIKE :suscritos");
$statement->execute(array(":suscritos" => "%Yellow%"));
$todos_mis_mensajes = $statement->fetchAll();

ID结果:1、2和3

 $statement = $conexion->prepare("SELECT * FROM comentarios  WHERE suscritos LIKE :suscritos");
 $statement->execute(array(":suscritos" => "%Yellow_"));
 $todos_mis_mensajes = $statement->fetchAll();

ID结果:无结果

$statement = $conexion->prepare("SELECT * FROM comentarios  WHERE suscritos LIKE :suscritos");
$statement->execute(array(":suscritos" => "_Yellow%"));
$todos_mis_mensajes = $statement->fetchAll();

ID结果:3

只需输入不带通配符的确切“黄色”字样即可。

%表示0个或更多字符。
“%Yellow%”表示所有带有黄字的字符串。
“%Yellow”表示所有以黄色结尾的字符串。
“ Yellow%”表示所有以Yellow开头的字符串。

_表示必须正好包含1个字符。 “ _Yellow”所有在黄字前面带有字符的字符串。 即:aYellow,bYellow
不可接受:黄色,黄色

试试这个电话:

select * from comentarios where suscritos regexp '[[:<:]]Yellow[[:>:]]';

在此处找到有关MySQL正则表达式的更多信息: https : //dev.mysql.com/doc/refman/5.7/en/regexp.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM