在子查询中转义 % 符号
我在 mySQL 中有一个查询,旨在返回我们网站上使用的搜索词。是的,这是针对标签云的,是的,我知道它是鲻鱼:)
我们有一个管理页面,管理员可以在其中查看搜索词并选择将它们排除在云中之外。这些词进入“badWords”表。我们得到了一些像“foo%2525252525252520bar”这样的术语,但我们在排除这些术语时遇到了困难。
在伪代码中,获取云搜索项的查询是:
SELECT * FROM `searchTerms` WHERE `word` NOT IN ( SELECT `word` FROM `badWords` )
这工作正常,除非从子查询返回的项之一包含 %。有没有办法逃避整个子查询?我尝试过做
replace( SELECT `word` FROM `badWords`, '%', '\%' )
...但这显然在语法上不正确。
如果需要的话,我可以做两个查询,但想知道是否有办法按原样完成它。
谢谢!
==============================
更新:暂时关闭它,因为我认为错误出在其他地方。一旦我确定,我会报告,但如果这不是正确的问题,我不希望人们浪费时间在这里回答问题...对
迄今为止收到的两个答复都投了赞成票。谢谢,伙计们。
==============================
更新2:叹气没关系...无法关闭它:\
================================
最终更新:嗯,看起来转义值不是问题。管理页面会先传递 URL 中的值,然后再将其添加到 badWords 表中。通过 URL 传递值时,它会发生变化。所以添加到 badWords 中的实际上是“foo%25252525252520bar”(少了一个“25”序列)。如果我手动更新 badWords 中的值并添加回丢失的“25”,它将按预期工作。所以不需要更换或逃避任何东西。我只需要正确修复这些 URL 值即可。
================================
I have a query in mySQL that's meant to return search terms that are used on our site. Yes, this is for a tag cloud, and yes, I know it's a mullet :)
We've got an admin page where administrators can view search terms and choose to exclude them from showing up in the cloud. These words go into the "badWords" table. We've gotten some terms like "foo%2525252525252520bar", and we're having trouble getting those excluded.
In pseudocode, the query to get the search terms for the cloud is:
SELECT * FROM `searchTerms` WHERE `word` NOT IN ( SELECT `word` FROM `badWords` )
This works fine, unless one of the terms returned from the subquery has a % in it. Is there a way to escape the entire subquery? I've tried doing a
replace( SELECT `word` FROM `badWords`, '%', '\%' )
... but that's apparently not syntactically correct.
I can do two queries if need be, but wondered if there's a way to get it done as is.
Thanks!
==============================
UPDATE: closing this for now, as I think the error lies elsewhere. Will report back once I know for sure, but don't want folks wasting time answering the question here if it's not the correct question...
Upvoted both of the replies received so far. Thanks, guys.
==============================
UPDATE 2: sigh Nevermind... can't close it :\
==============================
FINAL UPDATE: Well, looks like escaping the value isn't the problem. The admin page passes the value in the URL before it's added to the badWords table. In passing the value via the URL, it changes. So what's added to badWords is actually "foo%25252525252520bar" (there's one less "25" sequence). If I manually update the value in badWords and add back the missing "25" it works as expected. So no need to replace or escape anything. I just need to fix those URL values properly.
==============================
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为
%
是你的问题。我认为您尝试在子查询本身(SELECT ...
)上使用REPLACE()
,而不是在列值(word
代码>)。试试这个:祝你好运!
I don't think the
%
is your problem here. I think that you're trying to useREPLACE()
on the subquery itself (SELECT ...
), and not on a column value (word
). Try this instead:Good luck!
我不太擅长 MySQL 语法,但 SQL Server 让你这样做:
注意:基本上我所做的就是将你的
REPLACE
移到一些 =) 希望这个有帮助。I'm not very good with MySQL syntax, but SQL Server let's you do it this way:
NOTE: Basically all I did was move your
REPLACE
over some =) Hope this helps.