SQL 服务器以字符串列表开始

SQL Server starts with list of strings

我以前使用 MySQL 并且有一个使用 REGEXP 的查询。我试图找到以某些字符开头的 site_ids,并使用了以下内容:

WHERE site_id REGEXP '^(AB|BC|AO|BO|CA|PAF|Z)'

基本上是尝试查找 site_id LIKE 'AB%' OR 'BC%'... 所在的行,但因为我有相当多的字符串要匹配,所以我想以一种不那么冗长的方式进行。

不幸的是SQL服务器似乎不喜欢这种语法,我得到一个错误:

An expression of non-boolean type specified in a context where a condition is expected, near 'REGEXP'.DB-Lib error message 4145, severity 15: General SQL Server error: Check messages from the SQL Server

有没有不使用 LIKE 'XX%' OR ... 堆的巧妙方法?

您可以结合使用 leftorin 来缩短您的查询。

WHERE left(site_id,2) in ( 'AB', 'BC', 'AO', 'BO', 'CA') or left(site_id, 3) = 'PAF' or left(site_id, 1) = 'Z'