有没有“LIKE"的组合?和“IN"在 SQL 中?

2023-07-18数据库问题
1

本文介绍了有没有“LIKE"的组合?和“IN"在 SQL 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 SQL 中,我(遗憾地)经常不得不使用LIKE"条件,因为数据库几乎违反了所有规范化规则.我现在无法改变这一点.但这与问题无关.

In SQL I (sadly) often have to use "LIKE" conditions due to databases that violate nearly every rule of normalization. I can't change that right now. But that's irrelevant to the question.

此外,我经常使用诸如 WHERE something in (1,1,2,3,5,8,13,21) 之类的条件来提高我的 SQL 语句的可读性和灵活性.

Further, I often use conditions like WHERE something in (1,1,2,3,5,8,13,21) for better readability and flexibility of my SQL statements.

有没有什么办法可以在不编写复杂的子选择的情况下将这两件事结合起来?

Is there any possible way to combine these two things without writing complicated sub-selects?

我想要像 WHERE something LIKE ('bla%', '%foo%', 'batz%') 一样简单的东西,而不是这样:

I want something as easy as WHERE something LIKE ('bla%', '%foo%', 'batz%') instead of this:

WHERE something LIKE 'bla%'
OR something LIKE '%foo%'
OR something LIKE 'batz%'

我在这里使用 SQl Server 和 Oracle,但我很感兴趣,如果这在任何 RDBMS 中都可行.

I'm working with SQl Server and Oracle here but I'm interested if this is possible in any RDBMS at all.

推荐答案

LIKE & 没有组合SQL 中的 IN,更不用说 TSQL (SQL Server) 或 PLSQL (Oracle).部分原因是全文搜索 (FTS) 是推荐的替代方案.

There is no combination of LIKE & IN in SQL, much less in TSQL (SQL Server) or PLSQL (Oracle). Part of the reason for that is because Full Text Search (FTS) is the recommended alternative.

Oracle 和 SQL Server FTS 实现都支持 CONTAINS 关键字,但语法仍然略有不同:

Both Oracle and SQL Server FTS implementations support the CONTAINS keyword, but the syntax is still slightly different:

WHERE CONTAINS(t.something, 'bla OR foo OR batz', 1) > 0

SQL 服务器:

WHERE CONTAINS(t.something, '"bla*" OR "foo*" OR "batz*"')

您查询的列必须是全文索引.

The column you are querying must be full-text indexed.

参考:

  • 使用 Oracle Text 构建全文搜索应用程序
  • 了解 SQL Server Full-文本

这篇关于有没有“LIKE"的组合?和“IN"在 SQL 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12