0

MySQLで約2000レコードを更新する必要があります

次の値を持つテーブル'my_table'の列'my_content'があります

Title: some title here<br />Author: John Smith<br />Size: 2MB<br />

3つの新しい列(my_title、my_author、my_size)を作成しました。次に、このように「my_content」のコンテンツを分離する必要があります。

'my_title'

some title here

'my_author'

John Smith

'my_size'

2MB

タイトルを想像できるように、著者とサイズは行ごとに常に異なります。

私が考えているのは、次のクエリを実行することですが、SQLクエリは得意ではなく、実際のクエリがどのようになるかわかりません。

これが私がやろうとしていることです:

Within 'my_content' find everything that starts with "title:..." and ends with "...<br />au" and move it to 'my_title'
Within 'my_content' find everything that starts with "thor:..." and ends with "...<br />s" and move it to 'my_author'
Within 'my_content' find everything that starts with "ize:..." and ends with "...<br />" and move it to 'my_size'

これを行うためのクエリの書き方がわかりません。

すべてのコンテンツが新しい列に入ると、「thor:」など、不要になったコンテンツを見つけて削除できます。

4

2 に答える 2

0

を使用INSTRして、区切り文字のインデックスを検索SUBSTRINGし、必要な部分を選択できます。したがって、たとえば、作成者は

SUBSTR(my_content,
       INSTR(my_content, "Author: ") + 8,
       INSTR(my_content, "Size: ") - INSTR(my_content, "Author: ") - 8)

<br/>および周囲の空白をトリミングするには、もう少し作業が必要です。

于 2012-06-20T01:03:42.567 に答える
0

以下をお試しください。

SELECT SUBSTRING(SUBSTRING_INDEX(mycontent,'<br />',1),LOCATE('Title: ',mycontent)+7) as mytitle, 
SUBSTRING(SUBSTRING_INDEX(mycontent,'<br />',2),LOCATE('Author: ',mycontent)+8) as myauthor, 
SUBSTRING(SUBSTRING_INDEX(mycontent,'<br />',3),LOCATE('Size: ',mycontent)+6) as mysize 
FROM mytable;
于 2012-06-20T01:52:16.817 に答える