Search code examples
phpmysqltrim

Remove the first white space from specified column in MySql


Let's create an imaginary table named music which has the columns id, artist and title.

The first 2 rows will be:

1|C. Dione|My heart will go one;
2| C. Dione|I drive all night;

Now apparently both of data are correctly, but if We will use them into an jquery autocomplete using SELECT DISTINCT(artist) will give as 2 artists

C. Dione

and

 C. Dione 

How to remove the first white space in the artist column?


Solution

  • You can use:

    SELECT DISTINCT(LTRIM(artist)) ....
    

    Or you could also do,

    UPDATE music set artist = LTRIM(artist)
    

    which will prevent you from doing LTRIM everytime in the SELECT