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

时间:2012-02-08 20:42:31

标签: mysql substring

我有这个问题:

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'))

给了我这个错误:

#1630 - FUNCTION Database.SUBSTRING does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual

如果我只使用此查询,则查询有效:

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

3 个答案:

答案 0 :(得分:8)

根据MySQL Function Name Parsing,内置函数名称(在你的情况下是'SUBSTRING')和下面的“(”括号字符)之间必须没有空格。这是一个默认的解析器行为来区分是否有名称内置函数在非表达式上下文中用作函数调用或标识符。

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

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'))

答案 1 :(得分:5)

你不能在SUBSTRING和(。如果你真的想要的话,有一个db选项可以允许它)。

答案 2 :(得分:4)

你在最后一句中有一个额外的空间:

AND (SUBSTRING (phone,5,4) BETWEEN '5400' AND '5499'))
              ^---here

删除它,整个查询都可以。