简体   繁体   English

正则表达式替换为MySql Select查询

[英]Regex Replace in MySql Select query

I have this SQL query: 我有这个SQL查询:

SELECT * FROM pages WHERE page_title = 'paul_mccartney'

But often in page_title the value is like ' paul_mccartney (musician) '. 但通常在page_title该值类似于' paul_mccartney (musician) '。 And in this case I get NULL back. 在这种情况下,我会返回NULL I had the idea to regex replace everything which is between ( and ) including the () : 我的主意是用正则表达式替换()之间的所有内容,包括()

SELECT * FROM pages WHERE REPLACE(REGEXP('/\([^\*].*\)/U'), '', page_title) = 'paul_mccartney'

But it doesn't work. 但这是行不通的。 Is my idea possible or not? 我的想法可能吗? And how? 如何?

Although your question is about using RegEx but have you looked into MySQL fulltext search functions? 尽管您的问题是关于使用RegEx的,但是您是否研究过MySQL全文搜索功能?

https://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html https://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html https://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html https://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html

Using the following tutorials if you have a row that is like this: 如果您有这样的行,请使用以下教程:

Id  title
1   paul_mccartney (musician)

Using the example SQL provided will return a result: 使用提供的示例SQL将返回结果:

SELECT * FROM articles
WHERE MATCH (title) AGAINST ('paul_mccartney' IN BOOLEAN MODE)

您可以尝试一个LIKE ,看看它是如何工作的:

SELECT * FROM pages WHERE page_title LIKE '%paul_mccartney%'

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

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