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

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.

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

mysql> create table demo26
−> (
−> value text
−> );
Query OK, 0 rows affected (2.04 sec)

挿入コマンド-

を使用して、いくつかのレコードをテーブルに挿入します。
mysql> insert into demo26 values('This is my first MySQL query. This is the first tutorial. I am learning for the first time.');
Query OK, 1 row affected (0.10 sec)

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

mysql> select *from demo26;

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

+---------------------------------------------------------------------------------------------+
| value                                                                                       |
+---------------------------------------------------------------------------------------------+
| This is my first MySQL query. This is the first tutorial. I am learning for the first time. |
+---------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

以下は、最初のオカレンスのみを置き換えるクエリです-

mysql> update demo26
−> set value = REGEXP_REPLACE(value, 'first', 'second', 1, 1 ) ;
Query OK, 1 row affected (0.19 sec)
Rows matched: 1 Changed: 1 Warnings: 0

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

mysql> select *from demo26;

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

+----------------------------------------------------------------------------------------------+
| value                                                                                        |
+----------------------------------------------------------------------------------------------+
| This is my second MySQL query. This is the first tutorial. I am learning for the first time. |
+----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

  1. 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. テーブルを作成しましょう-

  2. C#で文字列の最初の10文字を見つける方法は?

    最初の10文字を取得するには、substring()メソッドを使用します。 以下が私たちの文字列だとしましょう- string str = "Cricket is a religion in India!"; ここで、最初の10文字を取得するには、以下に示すように、substring()メソッドに値10を設定します- string res = str.Substring(0, 10); 完全なコードを見てみましょう。 例 using System; public class Demo {    public static void Main() {