简体   繁体   English

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

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

I have a database table with two columns, one for the ID and another one for Colors. 我有一个包含两列的数据库表,一列用于ID,另一列用于Colors。 Imagine that I have 3 lines in this table with the following data: 假设我在此表中有3行,包含以下数据:

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

If I want to make a SQL call to know if there is a register with yellow color (just Yellow, no GreenYellow or Yellowgreen). 如果我想进行SQL调用以了解是否有黄色的寄存器(仅黄色,没有GreenYellow或Yellowgreen)。 How can I do it? 我该怎么做? I´m trying with: 我正在尝试:

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

IDs results: 1, 2 and 3 ID结果:1、2和3

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

IDs results: No result ID结果:无结果

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

IDs results: 3 ID结果:3

Just put the exact "Yellow" word with no wildcards. 只需输入不带通配符的确切“黄色”字样即可。

% indicates 0 or more characters. %表示0个或更多字符。
"%Yellow%" indicates all strings with Yellow word. “%Yellow%”表示所有带有黄字的字符串。
"%Yellow" indicates all string that ends in Yellow. “%Yellow”表示所有以黄色结尾的字符串。
"Yellow%" indicates all string that starts with Yellow. “ Yellow%”表示所有以Yellow开头的字符串。

_ indicates must have exactly 1 character. _表示必须正好包含1个字符。 "_Yellow" all strings that have a character before the Yellow word. “ _Yellow”所有在黄字前面带有字符的字符串。 ie: aYellow, bYellow 即:aYellow,bYellow
unacceptable: abYellow, Yellow 不可接受:黄色,黄色

Try this call: 试试这个电话:

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

Find more here about MySQL regular expressions: https://dev.mysql.com/doc/refman/5.7/en/regexp.html 在此处找到有关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