MySQL - 在WHERE子句中使用REPLACE()

时间:2018-04-16 19:36:14

标签: mysql sql replace where-clause

我知道我可以使用SELECT来替换所有空格,从而SELECT REPLACE(colm, ' ', '') FROM tbl列中的数据,但我怎样才能在WHERE子句中执行此操作?这就是我的尝试:

SELECT things.*, stores.* 
FROM things, stores 
WHERE things.id = 283468234 
&& 
stores.name = "Some Name" 
&& 
REPLACE(LOWER(stores.owner), ' ', '') = "firstnamelastname"

我想要的结果的一个例子是:

|  things.id  |   stores.name    |     stores.owner     |
|-------------|------------------|----------------------|
|  283468234  |    Some Name     |  First Name Last Name|

我得到的错误是:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 25' at line 1

注意:我还尝试使用AND代替&&,但没有改变

我认为我收到此错误的原因是由于PHPMyAdmin自动删除了我的上一个&&语句,并添加了LIMIT 0, 25,使查询SELECT things.*, stores.* FROM things, stores WHERE things.id = 283468234 && stores.name = "Some Name" && LIMIT 0, 25。这是问题还是我的查询?我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

&&相当于SQL中的AND

确保LIMIT是SQL语句中的最后一个子句 您对LIMIT的使用也是错误的。你想要:

LIMIT 25返回了25个结果 不是LIMIT 0, 25
(结果0到25是你的意图,我猜)。

从原始陈述中将它们全部放在一起:

SELECT things.*, stores.* 
FROM things, stores 
WHERE things.id = 283468234 
AND stores.name = "Some Name" 
AND REPLACE(LOWER(stores.owner), ' ', '') = "firstnamelastname"
LIMIT 25

这应该有效,但我还没有测试过。

答案 1 :(得分:0)

我发现了这个问题。看起来它纯粹是PHPMyAdmin的一个错误。我尝试用我的软件运行查询,它运行完美。