且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 WHERE 子句中有多个条件的 mySQL SUBSTRING

更新时间:2022-06-03 18:03:58

根据 MySQL 函数名解析 内置函数名(在你的例子中是'SUBSTRING')和后面的("括号字符之间不能有空格.这是一个默认的解析器行为,用于区分内置函数的名称被用作函数调用或非表达式上下文中的标识符.

According to the MySQL Function Name Parsing there must be no whitespace between the built-In function name ('SUBSTRING' in your case) and the following "(" parenthesis character. It's a default parser behaviour to distinguish whether names of the built-in functions are being used as function calls or as identifiers in nonexpression context.

因此,如果您在最后一行中删除SUBSTRING"函数调用后的空格,您的查询将正常工作:

So if you remove the white space after 'SUBSTRING' function call in the last line, your query will work fine:

SELECT DISTINCT phone, department 
FROM `users` 
WHERE ((SUBSTRING(phone,1,3) = '399') 
AND (SUBSTRING(phone,5,4) BETWEEN '3400' AND '3499')
OR  (SUBSTRING(phone,1,3) = '244') 
AND (SUBSTRING(phone,5,4) BETWEEN '5400' AND '5499'))