在MySQL查询中删除字符串的所有字符(除了第一个字符)

时间:2021-03-09 13:18:05

I am trying to run a query directly in MySQL to run through a series of records and replace the value of field with just the field character of the row.

我正在尝试在MySQL中直接运行查询,以遍历一系列记录,并用行的字段字符替换字段的值。

e.g based on a column called formname

e。g基于名为formname的列

row 1 - Renault
row 2 - Citreon
row 3 - Jaguar

That will return..

返回. .

row 1 - R
row 2 - C
row 3 - J

I can do this easily using native PHP functions but unsure how to do this using a native MYSQL function SUBSTR function?

我可以很容易地使用本地PHP函数,但是不知道如何使用本地MYSQL函数SUBSTR函数来实现这一点?

2 个解决方案

#1


4  

You can use left

您可以使用左

update table
set column = left(column,1)

or for select you can write

或者选择你可以写

select col1,left(col2,1)
from table

Demo

演示

#2


1  

To get this with the SUBSTR() function like you are asking for, you would use:

要使用SUBSTR()函数来实现这一点,您可以使用:

SELECT Field1, SUBSTR(field2, 1, 1) 
FROM MyTable

The function definition: SUBSTR(str,pos,len)

函数定义:SUBSTR(str、pos、兰)

#1


4  

You can use left

您可以使用左

update table
set column = left(column,1)

or for select you can write

或者选择你可以写

select col1,left(col2,1)
from table

Demo

演示

#2


1  

To get this with the SUBSTR() function like you are asking for, you would use:

要使用SUBSTR()函数来实现这一点,您可以使用:

SELECT Field1, SUBSTR(field2, 1, 1) 
FROM MyTable

The function definition: SUBSTR(str,pos,len)

函数定义:SUBSTR(str、pos、兰)