如何编写 A 包含(“a”或“b”)的 MySQL 查询

发布于 2024-11-09 10:59:59 字数 232 浏览 0 评论 0原文

我必须使用这种格式where A operand B。 A是场;我希望 B 为“text 1”或“text 2”,因此如果 A 具有“text 1 texttext”或“texttext 2”等数据,则查询将有结果。

但我该如何写这个呢? MySQL 是否支持类似的东西

where A contains ('text 1' OR 'text 2')? `

I must use this format where A operand B. A is the field; I want B to be either "text 1" or "text 2", so if A has data like "text 1 texttext" or "texttext 2" , the query will have result.

But how do I write this? Does MySQL support something like

where A contains ('text 1' OR 'text 2')? `

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

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

发布评论

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

评论(4

赠佳期 2024-11-16 10:59:59

两个选项:

  1. 使用 LIKE 关键字,以及字符串中的百分号

    从表中选择*,其中字段如“%a%”或字段如“%b%”。
    

    (注意:如果您的搜索字符串包含百分号,则需要对其进行转义)

  2. 如果您正在寻找比示例中指定的更复杂的字符串组合,您可以使用正则表达式 (regex):

    有关如何使用它们的更多信息,请参阅 MySQL 手册:http: //dev.mysql.com/doc/refman/5.1/en/regexp.html

其中,使用 LIKE 是最常用的解决方案——它是标准 SQL,并且在共同使用中。正则表达式不太常用,但功能更强大。

请注意,无论选择哪种选项,您都需要了解可能的性能影响。像这样搜索子字符串意味着查询必须扫描整个表。如果您有一个很大的表,这可能会导致查询非常慢,而且再多的索引也无济于事。

如果这对您来说是一个问题,并且您需要一遍又一遍地搜索相同的内容,您可能更喜欢执行一些操作,例如向表中添加一个标志字段,该字段指定字符串字段包含相关的子字段字符串。如果在插入或更新记录时保持此标志字段为最新,则在要搜索时只需查询该标志即可。这可以被索引,并且将使您的查询更快。是否值得为此付出努力取决于您,这取决于使用 LIKE 的性能有多差。

Two options:

  1. Use the LIKE keyword, along with percent signs in the string

    select * from table where field like '%a%' or field like '%b%'.
    

    (note: If your search string contains percent signs, you'll need to escape them)

  2. If you're looking for more a complex combination of strings than you've specified in your example, you could regular expressions (regex):

    See the MySQL manual for more on how to use them: http://dev.mysql.com/doc/refman/5.1/en/regexp.html

Of these, using LIKE is the most usual solution -- it's standard SQL, and in common use. Regex is less commonly used but much more powerful.

Note that whichever option you go with, you need to be aware of possible performance implications. Searching for sub-strings like this will mean that the query will have to scan the entire table. If you have a large table, this could make for a very slow query, and no amount of indexing is going to help.

If this is an issue for you, and you'r going to need to search for the same things over and over, you may prefer to do something like adding a flag field to the table which specifies that the string field contains the relevant sub-strings. If you keep this flag field up-to-date when you insert of update a record, you could simply query the flag when you want to search. This can be indexed, and would make your query much much quicker. Whether it's worth the effort to do that is up to you, it'll depend on how bad the performance is using LIKE.

苏佲洛 2024-11-16 10:59:59

您可以像这样编写查询:

SELECT * FROM MyTable WHERE (A LIKE '%text1%' OR A LIKE '%text2%')

% 是通配符,这意味着它搜索 A 列包含 text1text2 的所有行

You can write your query like so:

SELECT * FROM MyTable WHERE (A LIKE '%text1%' OR A LIKE '%text2%')

The % is a wildcard, meaning that it searches for all rows where column A contains either text1 or text2

两个我 2024-11-16 10:59:59

我大部分时间都使用过 LIKE 选项,而且效果很好。
我只是想分享一下我最近使用 INSTR 函数的经历之一。不管让我考虑这个选项的原因是什么,重要的是用途是相似的:
instr(A, '文本 1') > 0 或 instr(A, '文本 2') > 0
另一种选择可能是:
(instr(A, '文本 1') + instr(A, '文本 2')) > 0

我会选择 LIKE '%text1%' OR LIKE '%text2%' 选项...如果没有希望其他选项有帮助

I've used most of the times the LIKE option and it works just fine.
I just like to share one of my latest experiences where I used INSTR function. Regardless of the reasons that made me consider this options, what's important here is that the use is similar:
instr(A, 'text 1') > 0 or instr(A, 'text 2') > 0
Another option could be:
(instr(A, 'text 1') + instr(A, 'text 2')) > 0

I'd go with the LIKE '%text1%' OR LIKE '%text2%' option... if not hope this other option helps

触ぅ动初心 2024-11-16 10:59:59

我用户搜索摩托车尺寸:

例如:
数据 =“轮胎周期尺寸 70 / 90 - 16”

我可以使用“70 90 16”进行搜索

$searchTerms = preg_split("/[\s,-\/?!]+/", $itemName);

foreach ($searchTerms as $term) {
        $term = trim($term);
            if (!empty($term)) {
            $searchTermBits[] = "name LIKE '%$term%'";
            }
        }

$query = "SELECT * FROM item WHERE " .implode(' AND ', $searchTermBits);

I user for searching the size of motorcycle :

For example :
Data = "Tire cycle size 70 / 90 - 16"

i can search with "70 90 16"

$searchTerms = preg_split("/[\s,-\/?!]+/", $itemName);

foreach ($searchTerms as $term) {
        $term = trim($term);
            if (!empty($term)) {
            $searchTermBits[] = "name LIKE '%$term%'";
            }
        }

$query = "SELECT * FROM item WHERE " .implode(' AND ', $searchTermBits);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文