简体   繁体   English

选择%s作为值字符的查询sql

[英]Selecting query sql with % as value character

Hello i'm having a table in a database like this: 你好,我在这样的数据库中有一个表:

╔══════════════════════════════════════════╗
║              v3_url_alias                ║
╠════╦═══════════════╦═════════════════════╣
║ id ║    query      ║       keyword       ║
╠════╬═══════════════╬═════════════════════╣
║  1 ║ product_id=20 ║   540-65R38-K_028   ║
║  2 ║ product_id=21 ║ 18.00R33-EM_DT-150% ║
╚════╩═══════════════╩═════════════════════╝

I'm using this table to make my urls friendly but as many know a % sign ain't a valid url character therefore making my browser getting a bad request. 我正在使用此表格使我的网址友好,但很多人都知道%符号不是有效的网址字符,因此我的浏览器收到了错误的请求。 I have a lot of entries in this table (1700 or so) and need a query to grab all the ones with % in the column keyword. 我在这个表(1700左右)中有很多条目,需要一个查询来获取列关键字中包含%的所有条目。

So I've tried doing that in MySQL and came up with this query: 所以我尝试在MySQL中做到这一点并想出了这个查询:

SELECT * FROM v3_url_alias WHERE keyword LIKE '%%%';

This was returning all of my keywords as % is used as a wildcard. 这将返回我的所有关键字,因为%用作通配符。

My question how to retrieve every keyword containing a % character with SQL? 我的问题如何用SQL检索包含%字符的每个关键字?

Use an escape character! 使用转义字符!

MySQL has backslash ( \\ ) as default escape character: MySQL将反斜杠( \\ )作为默认转义字符:

SELECT * FROM v3_url_alias WHERE keyword LIKE '%\%%';

The ANSI SQL way is to use an ESCAPE clause to specify escape character, eg: ANSI SQL方法是使用ESCAPE子句指定转义字符,例如:

SELECT * FROM v3_url_alias WHERE keyword LIKE '%#%%' escape '#';

(This works with MySQL too, at least as long as \\ isn't specified.) (这也适用于MySQL,至少只要没有指定\\ 。)

Use square brackets. 使用方括号。

SELECT * 
FROM v3_url_alias 
WHERE keyword LIKE '%[%]%'

Using MySQL: 使用MySQL:

Use a backslash to escape character ( \\ ). 使用反斜杠转义字符(\\)。

SELECT * FROM v3_url_alias WHERE keyword LIKE '%\%%';

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

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