單查詢中的SQL查詢更新列值兩次(MySQL)

[英]SQL query update column value twice in single query (MySQL)


I have a text column named data which has value in format

我有一個名為data的文本列,其格式有值

"`set1:val1,val2|set2:val3`" 

so I have 2 sets field and n number of value fields in each set. I know this isn't normalized or proper approach to store data but it's a legacy project I can't change the schema now.

所以我在每組中有2組字段和n個值字段。我知道這不是規范化或正確的存儲數據方法,但它是一個遺留項目我現在無法更改架構。

Now I need a single query given two values for each sets say val4 and val5 I need my final data field to be

現在我需要一個查詢,給出每個集合的兩個值,例如val4和val5我需要我的最終數據字段

"`set1:val1,val2,val4|set2:val3,val5`"

I'm really not an expert in SQL query so need an update query for above. Note that I will always get only two values for each set and need to append in existing set values.

我真的不是SQL查詢的專家,所以需要上面的更新查詢。請注意,我將始終只為每個集合獲取兩個值,並且需要附加到現有的設置值中。

1 个解决方案

#1


1  

This is an ugly solution to an ugly problem

對於一個丑陋的問題,這是一個丑陋的解決方案

UPDATE table
SET data = CONCAT(REPLACE(data,'|',',val4|'), ',val5')
WHERE data = '...'

The REPLACE operation changes set1:val1,val2|set2:val3 to set1:val1,val2,val4|set2:val3, and then the CONACT operation changes that to set1:val1,val2,val4|set2:val3,val5

REPLACE操作將set1:val1,val2 | set2:val3更改為set1:val1,val2,val4 | set2:val3,然后CONACT操作將其更改為set1:val1,val2,val4 | set2:val3,val5


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2017/03/14/f3aeda5cf581778e85b9a9e577744c8c.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com