选择多行并组合/插入一行 [英] Select Multiple Rows and Combine / Insert into One Row

查看:91
本文介绍了选择多行并组合/插入一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我试图在我的MySQL数据库中合并这样的东西:


部门

=========

John帐户

Wayne帐户

James Management

债券管理


这样的事情:


姓名|部门

=========

约翰,韦恩账号

詹姆斯,债券管理


到目前为止,我发现通过运行MySQL脚本:

Hi All,

I am attempting to merge something like this in my MySQL database:

Name | Dept
=========
John Account
Wayne Account
James Management
Bond Management

into something like this:

Name | Dept
=========
John,Wayne Account
James,Bond Management

So far, I have found that by running the MySQL Script:

展开 | 选择 | Wrap | 行号

推荐答案

嗨。


你真的,真的不应该这样做。


你永远不应该把多于一个数据放入一个字段。这是一个非常糟糕的主意,因为在那种格式中,你的数据库无法控制它。

MySQL会认为它只是一个字符串,这会使你的数据更大的风险已损坏,并使前端软件实际使用数据变得更加复杂。


我建议你查看这篇文章。它解释了设计数据库时应始终牢记的3条基本规则。
http://bytes.com/topic/access/insigh...ble-structures


嗨Atli,


感谢您的回复。我确实理解数据库设计背后的基本原理,但是,在这种情况下,我使用的软件要求将数据组合成一列中的单行以使其起作用,因此关于如何进行操作的问题。 。:)


到目前为止我已经能够弄清楚如何使用GROUP_CONCAT,但遗憾的是,如何使用新的方法更新表的列和行数据还在我之外。


如果我要使用临时表,它会有帮助吗?例如:


表1

NAME | DEPT

==========

JOHN帐户

DOE帐户

JANE MANAGEMENT

能源管理


将新数据合并到一个新表中,如:


表2

NAME2 | DEPT2

===========

JOHN,DOE ACCOUNT

JANE,DOE MANAGEMENT
Hi Atli,

Thanks for the reply. I do understand the basic principle behind database design, however, in this case, the software I am using required that the data be combined into a single row in a single column for it to function, thus the question on how to do it... :)

I''ve so far been able to work out how to use GROUP_CONCAT, but unfortunately, how to tie in updating the table''s column and row with the new data is still beyond me.

If i was to use a temp table, will it help? eg:

Table1
NAME | DEPT
==========
JOHN ACCOUNT
DOE ACCOUNT
JANE MANAGEMENT
DOE MANAGEMENT

with the new data combined into a new table like:

Table2
NAME2 | DEPT2
===========
JOHN,DOE ACCOUNT
JANE,DOE MANAGEMENT


引入另一个表将导致需要将其内容与原始表同步。如果您的应用程序要求连接字段仅用于查看目的,则无需更改表。对于更新部分,您的前端语言应捕获数据并将其转换为后端所需的格式。

基本上数据库设计应该独立于数据需要如何呈现。在这种情况下,我认为您正在尝试更改后端以满足永远不应该执行的演示文稿格式。
Introducing the other table will give rise to the need to synchronize it''s contents with the original tables. If your application requires the fields to be concatenated for view purposes only then there is no need to change the tables. For the update part, your front end language should catch the data and convert it into the format required by the back end.
Basically database design should be done independently of how the data needs to be presented. In this case I think that you are trying to change the back end to satisfy a presentation format which should never be done.


这篇关于选择多行并组合/插入一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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