PHP、MySQL:重复一系列行,但更改 1 列?
我有一个名为scheduler_sched 的表,其中有多个列,其中包括一个名为schedule_id 的列。
我需要一个可以传递 2 个 id(copy_from_id、copy_to_id)作为参数的函数。我需要做的是获取 Schedule_id = copy_from_id 的每一行并复制它,但将 copy_from_id 更改为 copy_to_id
所以基本上我想要与此等效:
UPDATE scheduler_sched SET schedule_id = 32 WHERE schedule_id = 28
只是我不想更新任何行,我想创建重复项有了新的ID,
这有意义吗?
我该怎么做?
谢谢!
(顺便说一句,schedule_id 不是该表上的唯一/索引字段)
I have a table called scheduler_sched which has several columns, including a column called schedule_id.
I need a function where I can pass 2 ids (copy_from_id, copy_to_id) as parameters. And what I need to do is take every row where schedule_id = copy_from_id AND duplicate it but change the copy_from_id to the copy_to_id
So basically I want to to the equivalient of this:
UPDATE scheduler_sched SET schedule_id = 32 WHERE schedule_id = 28
Only I do not want to UPDATE any rows, I want to create duplicates with the new ID's
Does this make sense?
How can I do this?
THANKS!
(By the way schedule_id is not a unique/index field on this table)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为 ON DUPLICATE KEY UPDATE 语法可能对您有帮助:
http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html
例如:
I think that ON DUPLICATE KEY UPDATE syntax may help you:
http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html
e.g.:
只需插入新行而不是更新。如果schedule_id 28存在,则首先选择,如果存在,则插入一个新的schedule_id 28作为ID。
Just INSERT a new row instead of updating. SELECT first if that schedule_id 28 exists, and if it does, insert a new one with that being the ID.
由于您没有指定 MySQL 的版本,我将假设它是最新的 (5.4)。
假设我正确理解您,您应该能够使用触发器来实现此操作: http://dev.mysql.com/doc/refman/5.4/en/create-trigger.html
使用触发器的好处之一是它全部由数据库本身处理。
Since you haven't specified a version of MySQL, I'm going to assume that it is the lastest (5.4).
Assuming I am understanding you correctly, you should be able to implement this using triggers: http://dev.mysql.com/doc/refman/5.4/en/create-trigger.html
One of the benefits of using triggers, is it is all handled by the database itself.