SQL 类似和类似

发布于 2024-12-29 22:58:26 字数 138 浏览 1 评论 0原文

是否可以在一个查询中将多个 SQL LIKE 通配符串在一起 - 像这样?

LIKE '% aaaa %' AND LIKE '% bbbb %'

目的是查找包含两个通配符但没有特定顺序的记录。

Is it possible to string together multiple SQL LIKE wildcards in one query - something like this?

LIKE '% aaaa %' AND LIKE '% bbbb %'

The aim is to find records that contain both wild cards but in no specific order.

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

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

发布评论

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

评论(5

給妳壹絲溫柔 2025-01-05 22:58:27

正确的 SQL 语法是:

field LIKE '% aaaa %' AND field LIKE '% bbbb %'

The correct SQL syntax is:

field LIKE '% aaaa %' AND field LIKE '% bbbb %'
小嗷兮 2025-01-05 22:58:27

是的,这会起作用,但语法是:

Field LIKE '%aaa%' AND field LIKE '%bbb%'

Yes, that will work, but the syntax is:

Field LIKE '%aaa%' AND field LIKE '%bbb%'
木格 2025-01-05 22:58:27

当您将此语句与变量一起使用时,这非常有用。

SELECT * 
FROM `tableName`
WHERE `colName` LIKE CONCAT('%', 'aaaa', '%') AND -- if aaaa is direct Text
      `colName` LIKE CONCAT('%', 'bbbb', '%')

SELECT * 
FROM `tableName`
WHERE `colName` LIKE CONCAT('%', aaaa, '%') AND -- if aaaa is variable
      `colName` LIKE CONCAT('%', bbbb, '%')

This is useful when you are using this statement with variables.

SELECT * 
FROM `tableName`
WHERE `colName` LIKE CONCAT('%', 'aaaa', '%') AND -- if aaaa is direct Text
      `colName` LIKE CONCAT('%', 'bbbb', '%')

SELECT * 
FROM `tableName`
WHERE `colName` LIKE CONCAT('%', aaaa, '%') AND -- if aaaa is variable
      `colName` LIKE CONCAT('%', bbbb, '%')
野味少女 2025-01-05 22:58:27

是的,但请记住,LIKE 是类似于其他语言中的 ==> 的运算符。您仍然需要指定等式的另一边:

SELECT * FROM myTable
WHERE myField LIKE '%aaaa%' AND myField LIKE '%bbbb%'

Yes, but remember that LIKE is an operator similar to == or > in other languages. You still have to specify the other side of the equation:

SELECT * FROM myTable
WHERE myField LIKE '%aaaa%' AND myField LIKE '%bbbb%'
红颜悴 2025-01-05 22:58:27

可以将任意数量的条件串在一起。但是,谨慎的做法是使用括号对子句进行分组以消除任何歧义:

SELECT *
  FROM tableName
 WHERE (columnOne LIKE '%pattern%')
   AND (columnTwo LIKE '%other%')

It is possible to string together an arbitrary number of conditions. However, it's prudent to use parenthesis to group the clauses to remove any ambiguity:

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