开发者

MySQL Wildcard on Field Value?

开发者 https://www.devze.com 2023-03-22 17:39 出处:网络
I\'m trying to build a simple system for translating some values before they are outputted in a file for users. Let\'s say I have the text: \'Method (aaaabbbccc - random text)\', I\'m trying to let th

I'm trying to build a simple system for translating some values before they are outputted in a file for users. Let's say I have the text: 'Method (aaaabbbccc - random text)', I'm trying to let the user convert this to anything they want. I have built a table for conversions, and in this table the user would enter say 'Method *' => 'Description', which basically means they would like the text "Method" followed by anything (* = wildcard) to be simply translated into 'Description' (or any other text they would want).

If I had some info:

 Method (aaaabbbccc - random text)
 Method 2 (test - text)
 Method3

This should be converted into

Description
Description
Method3
开发者_C百科

As the last line does not satisfy the filters as defined by the user, it does not get converted.

I thought the simplest way to achieve this would be to perform a query for each row:

SELECT new_text FROM table WHERE old_text LIKE 'Method 2 (test - text)' etc.

But since old_text is 'Method *' the * needs to be converted into a wildcard and MySQL needs to handle this when performing the search. Every combination I try (even storing old_text as 'Method %') doesn't seem to work.

Hoping someone can provide some pointers for building a system like this where users can define filter and then have MySQL operate on those filters.


Did it in mysql /(using phpmyadmin). This is the resulting output screen (hope you interpret italian):

Visualizzazione record 0 - 1 (2 Totali, La query ha impiegato 0.0010 sec)

SELECT * FROM tabl WHERE txt LIKE 'method %'

id txt

        1   Method 2 (test - text)

        2   method (aaaabbbccc - random text)

Allright so?


I think you need something like this:

SELECT CASE WHEN old_text LIKE REPLACE(user_entry, '*', '%')
            THEN new_text
            ELSE old_text
       END AS modified_text 
FROM table 

where:
old_text is 'Method (aaaabbbccc - random text)',
user_entry is 'Method *' and
new_text is 'Description'

0

精彩评论

暂无评论...
验证码 换一张
取 消