MySQL 字段值通配符?

发布于 2024-11-25 17:36:27 字数 735 浏览 0 评论 0原文

我正在尝试构建一个简单的系统,用于在将某些值输出到用户的文件中之前对其进行翻译。假设我有文本:“方法(aaaabbbccc - 随机文本)”,我试图让用户将其转换为他们想要的任何内容。我已经构建了一个转换表,在该表中用户将输入“Method *”=>; “描述”,这基本上意味着他们希望将文本“方法”后跟任何内容(* = 通配符)简单地翻译为“描述”(或他们想要的任何其他文本)。

如果我有一些信息:

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

这应该转换为

Description
Description
Method3

由于最后一行不满足用户定义的过滤器,因此它不会被转换。

我认为实现这一点的最简单方法是对每一行执行查询:

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

但是由于 old_text 是“Method *”,因此 * 需要转换为通配符,并且 MySQL 在执行搜索时需要处理这个问题。我尝试的每个组合(甚至将 old_text 存储为“Method %”)似乎都不起作用。

希望有人可以提供一些指导来构建这样的系统,用户可以定义过滤器,然后让 MySQL 对这些过滤器进行操作。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

土豪我们做朋友吧 2024-12-02 17:36:27

在 mysql /(使用 phpmyadmin)中完成。这是结果输出屏幕(希望您能解释意大利语):

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

SELECT *
来自
WHERE txt LIKE 'method %'

id txt

        1   Method 2 (test - text)

        2   method (aaaabbbccc - random text)

好吧?

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?

小鸟爱天空丶 2024-12-02 17:36:27

我认为你需要这样的东西:

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

哪里:
old_text“方法(aaaabbbccc - 随机文本)”
user_entry'Method *' 并且
new_text'描述'

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'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文