Is it possible to combine SQL statements such as
SELECT * FROM X WHERE Y LIKE '%a%' OR Y LIKE '%b%' OR Y LIKE '%c%' OR Y LIKE '%d%'
with something like
SELECT * FROM X WHERE Y IN ('a', 'b', 'c', 'd')
so I don't have to write one big statement such as it is now:
IF NOT EXISTS(SELECT * FROM X WHERE Y LIKE '%a%' OR Y LIKE '%b%' OR Y LIKE '%c%' OR Y LIKE '%d%')
BEGIN
/* code */
END
Would be very nice to use something like SELECT * FROM X WHERE Y IN LIKE ('%a%', '%b%', etc..)
Appreciate all help and suggestions, thanks.
请使用以下代码解决您的问题,如果仍有问题,请尝试让我知道。
SELECT * FROM X WHERE Y LIKE '%[a-d]%'
If your database supports RegEx, you can use that. For example:
SELECT * FROM X WHERE Y SIMILAR TO '%(a|b|c|d)%'
in PostgreSQL, and
SELECT * FROM X WHERE Y REGEXP 'a|b|c|d'
in MySQL.
LIKE '%[ad]%'
won't work because LIKE
s in most databases don't accept regex patterns. This will just expect the literal text [ad]
somewhere in the string. How each database interprets regular expression (or native pattern) varies a lot. So you need to tailor these things to your database. For example, the following will not work in PostgreSQL:
SELECT * FROM X WHERE Y SIMILAR TO 'a|b|c|d'
because PostgreSQL will expect the whole string to be 'a', 'b', 'c', or 'd'.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.