简体   繁体   English

如何使用同一列中超过100列的多个列中的值插入行? 可能使用LOOP吗?

[英]How to insert rows using values from mulitple columns in the same table where there are over 100 columns? Possible use LOOP?

enter code here 在这里输入代码

Lets say i have the following table: 可以说我有下表:

ID Name    ID2 ID3 ID4 ID100
1   Smith   2   3   4   100
20  Joe     10  20  30  200
30  Jane    40  50  60  300

How can i write a statement that goes thru columns ID2 - ID100 one at a time and take the values from the column and the Name column and adds a new row to the table? 我如何编写一次一次通过ID2-ID100列并从列和Name列中获取值并向表中添加新行的语句?

To look like so: 看起来像这样:

ID Name    ID2 ID3 ID4 ID100
1   Smith   2   3   4   100
2   Smith               
3   Smith               
4   Smith               
100 Smith

Ive done this, but done want to write 100 select lines. 我已经做到了,但是确实想写100条选择线。

INSERT INTO mytable
(ID, Name)
SELECT ID2, Name FROM mytable WHERE Name = 'Smith'
UNION ALL
SELECT ID3, Name FROM mytable WHERE Name = 'Smith'
UNION ALL
SELECT ID4, Name FROM mytable WHERE Name = 'Smith';

Your database violates First Normal Form by creating repeating groups. 您的数据库通过创建重复组而违反了“第一范式”。 An RDBMS is not designed to handle this design cleanly. RDBMS并非旨在干净地处理此设计。 This is precisely why you don't want to create repeating groups. 这就是为什么您不想创建重复组的原因。 Any solution other than redesign will be ugly and slow. 除了重新设计之外,任何其他解决方案都将是丑陋且缓慢的。

The answer, if redesign isn't an option, is to do the painful part once and then not need do it again. 如果不能进行重新设计,答案是做一次痛苦的事情,然后就不必再做一次。

Create the following view: 创建以下视图:

CREATE VIEW MyUglyTable_Unpivot (ID, Name, Field_ID, Field_Value) AS
SELECT ID, Name, 'ID2',   ID2   FROM MyTable WHERE ID2   IS NOT NULL UNION ALL
SELECT ID, Name, 'ID3',   ID3   FROM MyTable WHERE ID3   IS NOT NULL UNION ALL
SELECT ID, Name, 'ID4',   ID4   FROM MyTable WHERE ID4   IS NOT NULL UNION ALL
SELECT ID, Name, 'ID5',   ID5   FROM MyTable WHERE ID5   IS NOT NULL UNION ALL
SELECT ID, Name, 'ID6',   ID6   FROM MyTable WHERE ID6   IS NOT NULL UNION ALL
SELECT ID, Name, 'ID7',   ID7   FROM MyTable WHERE ID7   IS NOT NULL UNION ALL
SELECT ID, Name, 'ID8',   ID8   FROM MyTable WHERE ID8   IS NOT NULL UNION ALL
SELECT ID, Name, 'ID9',   ID9   FROM MyTable WHERE ID9   IS NOT NULL UNION ALL
...
SELECT ID, Name, 'ID99',  ID99  FROM MyTable WHERE ID99  IS NOT NULL UNION ALL
SELECT ID, Name, 'ID100', ID100 FROM MyTable WHERE ID100 IS NOT NULL

Obviously, you can pick more sensible names for the view and the fields. 显然,您可以为视图和字段选择更合理的名称。 You can leave out the WHERE XXXX IS NOT NULL if the underlying table has NOT NULL constraints on those fields, but in my experience they don't. 如果基础表在这些字段上NOT NULL约束,则WHERE XXXX IS NOT NULL ,但以我的经验来看,它们不是。 You could also leave it out if you know you really need explicit NULL values, but that's generally considered poor design. 如果您知道确实需要显式的NULL值,也可以将其省略,但这通常被认为是较差的设计。 NULL is too ambiguous. NULL太含糊。

The view will be slow as hell, but it will let you do operations like this more easily. 视图将像地狱般缓慢,但是它将使您更轻松地执行此类操作。 If you're able to redesign your system to not use repeating groups, I strongly encourage you to do so. 如果您能够重新设计系统以不使用重复组,则强烈建议您这样做。

Now you can do this: 现在您可以执行以下操作:

INSERT INTO MyTable (ID, Name)
SELECT Field_Value, Name FROM MyUglyTable_Unpivot
WHERE Name = 'Smith'

Although, I'm thinking you need to have Field_ID in MyTable somewhere to make sense of it all. 虽然,我认为您需要在MyTable某个位置具有Field_ID才能使这一切有意义。

如果您使用外键概念,则可以使用excel来写那些选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历DatagridView中的行和列以将其值插入mysql表 - How to loop through rows and columns in a DatagridView to insert their values in a mysql table 如何从同一表的列中插入逗号分隔的值? - How do insert comma separated values from columns in the same table? 如何通过 MySQL 中的外键将多行值连接到第二个表中的列中 - How to Join mulitple rows of values into columns in second table, by foreign key in MySQL 如何从第二列值插入相同的列mysql - how to insert same columns from second columns values mysql PHP MySQL使用where子句INSERT来自另一个表的数据,并将唯一值插入到相同的行中 - PHP MySQL INSERT data from another table using the where clause and insert unique values to the same rows 通过从两列中选择值来插入同一表中的一列 - insert by selecting values from two columns into a column from the same table MySQL 如何 select 同一张表上的多行和 WHERE 多列? - MySQL How to select multiple rows with WHERE multiple columns on the same table? 如何从MySQL表中获取多列具有相同值的所有行? - How to get all the rows from MySQL table which have same values for multiple columns? 在目标表具有更多列的mySQL表中插入行 - Insert rows into mySQL table where target table has more columns 使用另一个表中的列和新值“插入”语法 - “insert into” syntax using columns from another table and new values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM