本文介绍了MySQL附加字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将字符串附加到现有表值的末尾?假设我有下表:



可以说,玛丽亚·安德斯(Maria Anders)不仅住在柏林,而且住在东京.如何将字符串
东京"附加到ContactName = Maria Anders的City列中?

P.S.我也希望能够附加到null值上.因此最终只剩下一个城市.

How can I append a string to the end of an existing table value? Let's say I have the table below:



And let's say that Maria Anders not only lived in Berlin but also Tokyo. How would I append the string
" Tokyo" onto the City column where ContactName = Maria Anders?

P.S. I want to be able to append on to a null value as well. Thus ending up with just the one city.

推荐答案

结合使用 CONCAT IFNULL (以处理NULL情况):

UPDATE `table`
   SET `City` = IFNULL(CONCAT(`City`, " Tokyo"), "Tokyo")
 WHERE `ContactName` = "Maria Anders";


但是似乎是错误的.如果要使用多个城市值(多对多关系),则应该有一个单独的城市表和一个人与城市关系表.了解有关数据库规范化的信息.


However, appending seems wrong. If you want multiple City values (a many-to-many relationship), you should have a separate table for cities and a separate table for person-city relationships. Read about database normalisation.

这篇关于MySQL附加字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:05