简体   繁体   English

在MySQL SELECT中使用REPLACE()

[英]Using REPLACE() in MySQL SELECT

I apologise in advance for asking what I'm sure will prove to be a very simple question. 我提前道歉,问我肯定会证明这是一个非常简单的问题。

I have a MySQL (5.5) database that includes, amongst other things, a field for telephone numbers. 我有一个MySQL(5.5)数据库,其中包括一个电话号码字段。 I'm trying to create a statement that will search that field, stripping out any spaces. 我正在尝试创建一个将搜索该字段的语句,删除任何空格。 So searching for '0208' will return '020 8', '02 08', '0 208', ' 0208', etc. 因此,搜索'0208'将返回'020 8','02 08','0 208','0208'等。

And this is in Delphi XE2, in case that makes a difference. 这是在Delphi XE2中,如果有所作为。

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

...gives me an error... ......给我一个错误......

Invalid filter in WHERE clause WHERE子句中的过滤器无效

...and... ...和...

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

...gives me... ...给我...

Invalid field name. 字段名称无效。 General SQL error. 一般SQL错误。 Column not found. 未找到列。

...and I do actually need all fields returned anyway. ......而且我确实需要所有返回的领域。

May I ask for some assistance in correcting the syntax please. 我可以请求一些帮助来纠正语法。 If you need more information, don't hesitate to ask. 如果您需要更多信息,请不要犹豫。 Thanks very much for your time. 非常感谢你花时间陪伴。

EDIT: One potentially critical piece of information I missed out. 编辑:我错过了一条可能很重要的信息。 The table is actually a Sage database that I'm accessing through ODBC. 该表实际上是一个我通过ODBC访问的Sage数据库。 As nothing I try is working that may well be the root problem. 因为我尝试的任何工作都可能是根本问题。 Apologies for not saying that earlier. 抱歉不早点说。

Your Query Seems Working Fine see the demo 您的查询似乎工作正常,请参阅演示

First try this query to you DB Client 首先尝试此查询到您的DB客户端

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 ) 尝试运行一个简单的选择查询( SELECT * FROM sales_ledger
  • if the first query run try adding simple where condition. 如果第一个查询运行尝试添加简单where条件。 so step by step you can make your query similar to original one and find where is the actual error from. 因此,您可以逐步使您的查询与原始查询类似,并找到实际错误的位置。

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+"%'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM