mysql-在where子句中使用replace()

ukdjmx9f  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(568)

我知道我可以 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 . 这是问题还是我的疑问?我怎样才能解决这个问题?

lyr7nygr

lyr7nygr1#

&& 相当于 AND 在sql中。
https://stackoverflow.com/a/4105692/5129424
确保limit是sql语句中的最后一个子句。
你对极限的使用也是不正确的。你想要: LIMIT 25 返回25个结果
不是 LIMIT 0, 25 (结果0到25是你的意图,我想)。
https://dev.mysql.com/doc/refman/5.5/en/limit-optimization.html
把你最初的陈述加起来:

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

虽然我还没有测试过,但这应该是可行的。

qvk1mo1f

qvk1mo1f2#

我发现了问题。看起来这纯粹是phpmyadmin的错误。我试着用我的软件运行这个查询,它运行得非常完美。

相关问题