我有一个表,其中一些值存储在关键字列中,但关键字列中的值包含额外的空格,而检索数据查询由于这些额外的空间而无法获取所有记录。我尝试在mysql中使用trim(),但它对我无效
预期输出: mysql查询应通过避免where条件后面的额外空格来获取所有记录。
表:
id keyword
1 some text for product //here single space after 'some'
2 some text for product // here two space after 'some'查询:
SELECT * FROM `table` where trim(keyword)='some text for product'执行上述操作后,查询将只返回单个记录,而不是所有记录,因为列值中存在额外的空格。
发布于 2019-10-13 19:47:20
TRIM()删除字符串开头或结尾的空格。基本上,您希望将字符串中的双空格替换为单空格。您可以改用REPLACE():
SELECT * FROM `table` WHERE REPLACE(keyword, ' ', ' ') = 'some text for product'WITH `table` AS (
SELECT 'some text for product' keyword
UNION ALL SELECT 'some text for product' keyword
)
SELECT * FROM `table` WHERE REPLACE(keyword, ' ', ' ') = 'some text for product';
| keyword |
| ---------------------- |
| some text for product |
| some text for product |https://stackoverflow.com/questions/58362929
复制相似问题