复制同一个表中的行并仅更新 MySQL 中的一列
我需要根据某些条件(where 子句)从表中复制一行,在同一个表中创建重复条目并仅更新一列(外键)。但是因为“select * from Table_name”返回包括primary_key在内的所有字段,所以无法插入新行 错误:重复的键。
我不知道表的所有列,所以我不能通过在选择查询中仅给出所需的列名来忽略primary_key。
有什么方法可以忽略 Primary_key 本身的获取吗?或者我可以获取整行,并将 Primary_key 值设置为 null,然后将该行插入表中,以便它自动增加要添加的新行的 Primary_key 。
谢谢。
I need to copy a row based on some condition(where clause), from a table,make a duplicate entry in same table and update just one column(a foreign_key). But because "select * from Table_name" returns all fields including primary_key it is not able to insert new row
error : duplicate key.
I don't know all the columns of table, so i cant ignore primary_key by giving only required column names in select query.
Is there any way to ignore primary_key from fetching itself.. or can i fetch the whole row, and set the primary_key value to null, and then insert the row in table, so that it auto increments the primary_key for new row being added.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过查询信息模式表并使用动态 SQL 构建适当的语句,可以执行您想要的操作。但是,您应该非常小心地使用这种方法,因为修改您不知道列名的表中的数据可能会导致各种问题:
It is possible to do what you want by querying the information schema tables, and using dynamic SQL to build an appropriate statement. However, you should take great care with this approach, as modifying data in tables for which you do not know the column names could result all sorts of problems:
如果您了解所获得的数组的序列,那么在设置某种条件后,您可以忽略具有主键的字段。
像这样检查它是否有效
谢谢。
if you have knowledge about the sequences of the array you got, then after putting some kind of condition you can ignore field which is having primary key.
Check like this if it works
Thanks.
为什么你知道键是什么,但不知道其他列是什么?如果情况确实如此,那么我认为您必须编写一些动态 SQL - 或者更好的是,将任务交给确实了解表结构的人。
How can it be that you know what the key is but don't know what the other columns are? If that's really the case then I think you'll have to write some dynamic SQL - or better still, give the task to someone who does know the table structure.