MySQL
 Computer >> コンピューター >  >> プログラミング >> MySQL

MySQLを使用して文字列の一部(@の後のドメイン名)を置き換える方法は?


最初にテーブルを作成しましょう-

mysql> create table DemoTable
   -> (
   -> EmailId varchar(30)
   -> );
Query OK, 0 rows affected (0.53 sec)

挿入コマンド-

を使用して、テーブルにいくつかのレコードを挿入します
mysql> insert into DemoTable values('John123@example.com');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('John123@gmail.com');
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable values('John123@yahoo.com');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('John123@example.com');
Query OK, 1 row affected (0.10 sec)

selectステートメントを使用してテーブルのすべてのレコードを表示する-

mysql> select *from DemoTable;

これにより、次の出力が生成されます-

+---------------------+
| EmailId             |
+---------------------+
| John123@example.com |
| John123@gmail.com   |
| John123@yahoo.com   |
| John123@example.com |
+---------------------+
4 rows in set (0.00 sec)

文字列の一部を置き換えるクエリは次のとおりです-

mysql> update DemoTable
   -> set EmailId=replace(EmailId,'John123@example.com','John123@gmail.com');
Query OK, 2 rows affected (0.16 sec)
Rows matched: 4 Changed: 2 Warnings: 0

テーブルレコードをもう一度確認しましょう-

mysql> select *from DemoTable;

これにより、次の出力が生成されます-

+-------------------+
| EmailId           |
+-------------------+
| John123@gmail.com |
| John123@gmail.com |
| John123@yahoo.com |
| John123@gmail.com |
+-------------------+
4 rows in set (0.00 sec)

  1. SUBSTRING_INDEXを使用してMySQLで文字列を分割する方法は?

    まずテーブルを作成しましょう- mysql> create table DemoTable1465    -> (    -> Name varchar(40)    -> ); Query OK, 0 rows affected (0.54 sec) 挿入コマンド-を使用して、テーブルにいくつかのレコードを挿入します mysql> insert into DemoTable1465 values('Chris Brown'); Query OK, 1 row affected (0.1

  2. MySQLで文字列の最初に繰り返される値のみを置き換える方法

    これには、REGEXP_REPLACE()を使用できます。文字列が-だとしましょう This is my first MySQL query. This is the first tutorial. I am learning for the first time. 特定の単語の最初の出現のみを置き換える必要があります。たとえば、「最初」としましょう。出力は-である必要があります This is my second MySQL query. This is the first tutorial. I am learning for the first time. テーブルを作成しましょう-