如何更新表中的记录,以便在sql中插入逗号和值及其值? [英] how to update a record in the table for inserting a comma and a value and its value in sql?

查看:78
本文介绍了如何更新表中的记录,以便在sql中插入逗号和值及其值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含字段的表区域:国家和城市

如下所示:



表区域



kode |城市

--------------



indonesia |雅加达



马来西亚| kuala lumpur



i希望用逗号插入记录城市国家印度尼西亚

结果:



country |城市

--------------

印度尼西亚|雅加达,万隆,jogja



马来西亚| kuala lumpur





如何创建一个sql查询?

i have a table region with fields : country and city
like below :

table region

kode | city
--------------

indonesia | jakarta

malaysia | kuala lumpur

i want to insert record city with comma where country indonesia
the result :

country | city
--------------
indonesia | jakarta, bandung, jogja

malaysia | kuala lumpur


how to create a sql query for that ??

推荐答案

可以但不应该这样做。切勿在单个单元格中放置多个值。这是违反1NF原则的。在关系数据库设计中,每个单元格不能使用多个值,例如用逗号分隔。它们应分成多行。

阅读更多:我遵循的11个重要的数据库设计规则 [ ^ ]
It can but should not be done. Never put multiple values in a single cell. It is against 1NF principle. In relational database design, every cell must not take more than one value, like separated with commas. They should be separated into multiple rows.
Read more: 11 important database designing rules which I follow[^]


Peter提到了一个非常有效的观点。但是,如果你必须这样做,这就是方法:



UPDATE Region

SET City ='jakarta,bandung,jogja '

WHERE country ='Indonesia'
Peter mentioned a very valid point. But, in case you have to do it, here is the way:

UPDATE Region
SET City = 'jakarta, bandung, jogja'
WHERE country = 'Indonesia'


这篇关于如何更新表中的记录,以便在sql中插入逗号和值及其值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆