[英]MySQl update column with values in a specific format
我正在使用一個舊的應用程序和mysql db,可悲的是,這里有些混亂。
我對導出所需的列感興趣,該列包含特定文檔的編輯器名稱。 此列中的值應以特定方式設置格式,並且應與當前不同的值保持一致。
一個文檔可以有許多編輯器。 我有一個表,其中包含所有需要與當前(格式錯誤的)編輯器值進行匹配的不同編輯器。 我知道這不是執行此操作的最佳方法(這是應用程序當前的工作方式),但我需要將editors列的格式設置如下:
"Surname, Initial. and Surname, Initial. and Surname, Initial"
例如:
"Bloggs, J. and Doe, J."
"Smith, M.J. and Bloggs, J. and Jones, P"
"Williams, S. and Smith, M.J. Doe, J."
但是,該列當前不一致,沒有實際的格式設置:
"J Bloggs, J Doe"
"MJ Smith, J Bloggs and P Jones"
"Williams, S, M.J Smith. Doe, J."
等等...
這是我當前的表格:
文件 :
| title | editors |
------------------------------------------------
| doc title 1 | J Bloggs, J Doe |
| doc title 2 | M Smith, J Bloggs and P Jones |
| doc title 3 | Williams, S, M Smith. Doe, J. |
Eitors :
| initial | name |
------------------------
| J. | Bloggs |
| J. | Doe |
| M.J. | Smith |
| P. | Jones |
| S. | Williams |
為了進行測試,我在Documents表中添加了一列(formatted_editors):
| title | editors | formatted_editors |
---------------------------------------------------------------------------------
我需要看起來像這樣:
| title | editors | formatted_editors |
---------------------------------------------------------------------------------
| doc title 1 | J Bloggs, J Doe | Bloggs, J. and Doe, J.
| doc title 2 | M Smith, J Bloggs and P Jones | Smith, M.J. and Bloggs, J. and Jones, P
| doc title 3 | Williams, S, M Smith. Doe, J. | Williams, S. and Smith, M.J. Doe, J.
我嘗試了以下方法(以及其他方法),但似乎運氣不佳-我認為這可能與我的分組有關?
SELECT d. title, d.editors, group_concat(e.name, ', ', e.initial SEPARATOR ' and ') as formatted_editors
FROM documents d
INNER JOIN editors e
ON d.editors LIKE concat('%', e.name, '%')
WHERE d.editors LIKE concat('%', e.name, '%')
and et.editors LIKE concat('%', e.initial, '%')
GROUP BY et.editors;
任何幫助/指針將不勝感激,
那么,沒有“真正的”可靠的方法來解決這個問題。。。我用了一些基本的SQL來修正/匹配我所能做的,然后手動完成其余的工作。
謝謝,喬恩。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.