这可能是一个非常奇怪的问题但是这里有一个电视列表页面,显示了一个由Genre显示的列表。每个节目的My Genre列值如下所示(文本字符串):
Adventure, Action & Adventure, Sci-Fi & Fantasy
现在,如果我搜索冒险,我会得到正确的结果,但如果我之间有特殊字符,我会得不到任何结果,请参阅下文:
$query .= "
SELECT t1.*,
t2.content as genres
FROM tvshows as t1
LEFT JOIN tvshows_content as t2 ON t2.tvshows_id = t1.id AND t2.type = 'genres'
WHERE (t2.content LIKE "%,sci fi fantasy,%" OR t2.content LIKE "sci fi fantasy,%" OR t2.content LIKE "%,sci fi fantasy" OR t2.content = "sci fi fantasy" OR t2.content LIKE "%sci fi fantasy%" OR t2.content LIKE "sci-fi-fantasy" ) ";
我的问题是我删除了页面slu的特殊字符,但我想检查它们是否存在?这可能吗?
答案 0 :(得分:1)
如果你实际上找不到搜索'Action&冒险'那么这不是问题,你使用的是特殊字符,更有可能的是,你在字段和查询中使用不同的字符编码。您可以尝试使用BINARY
运算符,但您可以先尝试不同的优化。
查看您正在使用的SQL查询:
SELECT t1.*, t2.content as genres FROM tvshows as t1
LEFT JOIN tvshows_content as t2 ON t2.tvshows_id = t1.id AND t2.type = 'genres'
WHERE (
t2.content LIKE "%,sci-fi-fantasy,%" OR
t2.content LIKE "sci-fi-fantasy,%" OR
t2.content LIKE "%,sci-fi-fantasy" OR
t2.content = "sci-fi-fantasy" OR
t2.content LIKE "%sci-fi-fantasy%" OR
t2.content LIKE "sci-fi-fantasy"
)
你在那里做了很多工作以确保你正在搜索的类型可能在列表的开头或结尾。有两种不同的方法。一个不错的是FIND_IN_SET
,用于搜索逗号分隔的字符串:
SELECT t1.*, t2.content as genres FROM tvshows as t1
LEFT JOIN tvshows_content as t2 ON t2.tvshows_id = t1.id AND t2.type = 'genres'
WHERE FIND_IN_SET('sci-fi-fantasy',t2.content) > 0
但是在这里他必须确保你的查询字符编码匹配字段字符编码,如果你想搜索ie umlaut。尝试一下,如果它不起作用,或者你无法匹配你的编码,那么你可以使用另一种方式 - 稍微不那么优雅 - 通过事先将逗号与逗号字段相结合,这样你也只需要搜索对于一个变体:
SELECT t1.*, t2.content as genres FROM tvshows as t1
LEFT JOIN tvshows_content as t2 ON t2.tvshows_id = t1.id AND t2.type = 'genres'
WHERE CONCAT(',',t2.content,',') LIKE "%,sci-fi-fantasy,%"
但是在这里你也可以使用BINARY运算符,如果你需要变音符号,特殊字符等,并且无法匹配编码:
SELECT t1.*, t2.content as genres FROM tvshows as t1
LEFT JOIN tvshows_content as t2 ON t2.tvshows_id = t1.id AND t2.type = 'genres'
WHERE BINARY CONCAT(',',t2.content,',') LIKE "%,sci-fi-fantasy,%"
希望这有帮助!