0

I know, this could easly be done using PHP, but just curious,

How could I explode on mail field to select the value from alias column with the first exploded part by @?

Current:

+-------+-----------------------------------------+
| alias | mail                                    |
+-------+-----------------------------------------+
|       | user.one@gmail.com                      |
|       | user.two.foo@gmail.com                  |
+-------+-----------------------------------------+

Desired:

+-------+------------------------------------------------+
| alias        | mail                                    |
+-------+------------------------------------------------+
| user.one     | user.one@gmail.com                      |
| user.two.foo | user.two.foo@gmail.com                  |
+-------+------------------------------------------------+

In pseudo code would be like

update tablename set tablename.alias = explode('@', tablename.mail)[0];
Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378

1 Answers1

1

In MySQL, you can use substring_index():

update t
    set alias = substring_index(t.mail, '@', 1);
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786