简体   繁体   English

如何在mysql的同一列上使用LEFT和REPLACE?

[英]How can I use LEFT and REPLACE on the same column in mysql?

I want to select a column and remove(replace with space) any line feeds or carriage returns and I want to get only the first 40 characters in the column after the replace. 我想选择一个列并删除(替换为空格)任何换行或回车,我想在替换后只获得列中的前40个字符。

Here is the query I tried but I get 这是我试过的查询,但我得到了

select date, REPLACE(LEFT(message, '\r', ' '), 30)
from table1 
where message like 'testing%'

ERROR 1064 (42000): You have an error in your SQL syntax; 错误1064(42000):您的SQL语法有错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' ' '), 30) from events_cleared where message like 'testing%' ' at line 1 检查与您的MariaDB服务器版本对应的手册,以便在'''附近使用正确的语法,30)来自events_cleared,其中第1行的'testing%'消息

Replace needs the arguments '\\r; 替换需要参数'\\ r; and ' ' 和''

LEFT(REPLACE(message, '\r', ' '), 30)

or 要么

REPLACE(LEFT(message,30), '\r', ' ')

You have the ordering of your arguments incorrect (you have passed the REPLACE arguments to LEFT ). 您的参数排序不正确(您已将REPLACE参数传递给LEFT )。 Try this: 尝试这个:

select date, REPLACE(LEFT(message, 30), '\r', ' ')
from table1 
where message like 'testing%'

Your function identifiers are switched and therefore the number of arguments weren't right. 您的函数标识符已切换,因此参数的数量不正确。 if you want replace() to happen first is has to go deeper in the nesting than the left() . 如果你想首先发生replace() ,那么嵌套必须比left()更深入。 You also wrote 30 as second argument to left() which would give you only 30 characters, not 40. 你还写了30作为left()第二个参数,它只给你30个字符,而不是40个。

SELECT date,
       left(replace(message, '\r', ' '), 40)
       FROM table1
       WHERE message LIKE 'testing%';

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

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