在MySQL中我有以下WHERE子句:SQL LIKE也不行返回预期
WHERE `candidates`.`availability` = "yes"
AND `candidates`.`playing_age` LIKE "%child, 20's%"
在我的数据库,我有一个排,其中playing_age列看起来像这样,
“的孩子,青少年,20日的”
为什么会喜欢我的WHERE子句不返回该行,即使行包含儿童和20年代?
在MySQL中我有以下WHERE子句:SQL LIKE也不行返回预期
WHERE `candidates`.`availability` = "yes"
AND `candidates`.`playing_age` LIKE "%child, 20's%"
在我的数据库,我有一个排,其中playing_age列看起来像这样,
“的孩子,青少年,20日的”
为什么会喜欢我的WHERE子句不返回该行,即使行包含儿童和20年代?
%匹配任意数量的字符,甚至是零个字符。
当你做LIKE "%child, 20's%"
你缺少child,
和20's
之间的%
字符。所以,你需要加一个额外的%
:
WHERE `candidates`.`availability` = "yes"
AND `candidates`.`playing_age` LIKE "%child,%20's%"
这是不匹配的,应该是
"%child, %20's%"
mysql> select "child, teen, 20's" like "%child, 20's%"; +------------------------------------------+ | "child, teen, 20's" like "%child, 20's%" | +------------------------------------------+ | 0 | +------------------------------------------+ mysql> select "child, teen, 20's" like "%child, %20's%"; +-------------------------------------------+ | "child, teen, 20's" like "%child, %20's%" | +-------------------------------------------+ | 1 | +-------------------------------------------+
然而,您的设计方式的架构是相当不归,
分裂的孩子,青少年,20年代为三个单独的列(或3行如果使用EAV),
,你可以轻松做比较。
应该
WHERE `candidates`.`availability` = "yes"
AND `candidates`.`playing_age` LIKE "%child,%20's%"
要返回将不会返回,因为有一个在字符串中间没有通配符的结果。
LIKE“%child,20's%”意思是指任何完全包含“child,20's”的东西。 “后anyhting” 领导%的 尾随%“任何事情之前”为
什么是你SEACH更apporpriate将
AND candidates.playing_age REGEXP ".*child,.*20's.*"
这将使孩子& 20的
之间“的任何匹配”请注意,前导&尾随。*可能会被删除,结果仍然匹配。但后者不确定。