11

我提前道歉,因为我提出了一个我确信会被证明是一个非常简单的问题。

我有一个 MySQL (5.5) 数据库,其中包括电话号码字段。我正在尝试创建一个将搜索该字段的语句,去除任何空格。所以搜索“0208”将返回“020 8”、“02 08”、“0 208”、“0208”等。

这是在 Delphi XE2 中,以防万一。

'SELECT * FROM sales_ledger WHERE REPLACE(telephone, " ", "") LIKE "%' + SearchEdit.Text + '%"'

...给我一个错误...

WHERE 子句中的过滤器无效

...和...

'SELECT REPLACE(telephone, " ", "") FROM sales_ledger WHERE REPLACE(telephone, " ", "") LIKE "%' + SearchEdit.Text + '%"'

...给我...

无效的字段名称。一般 SQL 错误。未找到列。

...而且我确实需要返回所有字段。

我可以请求一些帮助来纠正语法吗?如果您需要更多信息,请随时询问。非常感谢你花时间陪伴。

编辑:我错过了一条潜在的关键信息。该表实际上是我通过 ODBC 访问的 Sage 数据库。由于我尝试的任何方法都不起作用,这很可能是根本问题。很抱歉没有早点说。

4

2 回答 2

8

Your Query Seems Working Fine see the demo

First try this query to you DB Client

SELECT * FROM sales_ledger 
WHERE REPLACE(telephone, " ", "") LIKE "%0208%"

EDIT:

if you query is still not working. fellow step by step process.

  • try to run a simple select query (SELECT * FROM sales_ledger)
  • if the first query run try adding simple where condition. so step by step you can make your query similar to original one and find where is the actual error from.
于 2012-12-28T12:03:22.963 回答
4

Try this ::

SELECT 
REPLACE(telephone,' ', '') as replacedColumn
FROM sales_ledger 


WHERE REPLACE(telephone,' ', '') LIKE '%"+ SearchEdit.Text +"%'"

OR

Select temp1.* 
from
(
SELECT 
REPLACE(telephone,' ', '') as replacedColumn
FROM sales_ledger 
) as temp1

WHERE temp1.replacedColumn LIKE '%"+SearchEdit.Text+"%'"
于 2012-12-28T11:57:21.180 回答