PHP、MySQL:重复一系列行,但更改 1 列?

发布于 2024-08-09 06:34:25 字数 429 浏览 6 评论 0原文

我有一个名为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风吹短裙飘 2024-08-16 06:34:25
Insert into scheduler_sched (column1, column2, column3,schedule_id ) 
Select column1, column2, column3, 32 from scheduler_sched WHERE schedule_id = 28
Insert into scheduler_sched (column1, column2, column3,schedule_id ) 
Select column1, column2, column3, 32 from scheduler_sched WHERE schedule_id = 28
抠脚大汉 2024-08-16 06:34:25

我认为 ON DUPLICATE KEY UPDATE 语法可能对您有帮助:

http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html

例如:

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE c=c+1;

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.:

INSERT INTO table (a,b,c) VALUES (1,2,3)
  ON DUPLICATE KEY UPDATE c=c+1;
彻夜缠绵 2024-08-16 06:34:25

只需插入新行而不是更新。如果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.

迟月 2024-08-16 06:34:25

由于您没有指定 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文