是否可以将一个表中的值插入到另一个表中并更新同一查询中的字段值?

发布于 2024-10-02 03:43:48 字数 284 浏览 4 评论 0原文

我有两张桌子。一张表用作交易历史记录,另一张表用于记录会员详细信息。运行报告时,我想将部分成员详细信息移至交易历史记录中,但还更新一些原本不存在的字段记录。

是否可以选择满足特定条件的所有记录,仅将匹配行的片段插入到另一个表中并在单个查询中更新其他字段?

例如:

在表 2 中,我有会员姓名、注册日期和会员 ID。我想将上述记录移至表 1 中,同时将字段(状态)更新为“已处理”。

注意:我还使用 php 和 pdo 连接到 mysql 数据库。

这可以在单个查询中实现吗?

I have two tables. One table is meant to serve as a transaction history and the other is a log of member details. When a report is run, I want to move pieces of the member details into the transaction history but ALSO update some field records which would not otherwise exist.

Is it possible to select all records which meet a specific criteria, insert only pieces of the matching row into another table AND update other fields in a single query?

For example:

In table 2, i have member name, date registered, and memberid. I want to move the above records into table 1 but also update the field (status) equal to 'processed'.

Note: I am also using php and pdo to connect to a mysql database.

Is this possible within a single query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

醉梦枕江山 2024-10-09 03:43:48

您没有指定要更新的行是否与要插入的行相同。我假设他们是:

insert into table1
(member_name, date_registered, memberid, status)
select member_name, date_registered, memberid, 'processed'
from table2
where SomeField = MyCriteria

You didn't specify whether the rows you want to update are the same as the ones you are inserting. I am assuming they are:

insert into table1
(member_name, date_registered, memberid, status)
select member_name, date_registered, memberid, 'processed'
from table2
where SomeField = MyCriteria
顾北清歌寒 2024-10-09 03:43:48

经过深思熟虑 - 我决定使用 ircmaxell 的建议并简单地运行多个查询。它最终不仅使事情变得更容易,而且使我能够更轻松地自定义排序。

正如他上面所说,“不要陷入越少越好的陷阱”

After much consideration - I decided to use ircmaxell's advice and simply run multiple queries. It ends up not only making things easier but allows me to customize my sorting much easier.

As he said above, "Don't get caught in the trap of less is always better"

泅渡 2024-10-09 03:43:48

是的:

SELECT *, "processed" INTO table2 FROM table1

您必须根据表结构进行调整,甚至可能写出所有字段:

SELECT field1, field2, field3, "processed" INTO table2 FROM table1

值得注意的是,这假设您想要写入表 2包括已处理的变量(我可能建议使用布尔值) ?)如果你想要另一个表中的“已处理”,它会变得更加复杂。

编辑:显然 mysql 不支持 select into 所以...

INSERT INTO table2 SELECT field1, field2, field3, "processed" FROM table1

Redfilters 代码可以工作

Yes:

SELECT *, "processed" INTO table2 FROM table1

You will have to adapt based on the table structures, perhaps even write out all the fields:

SELECT field1, field2, field3, "processed" INTO table2 FROM table1

Of note, this assumes you want to write into table 2 including the processed variable (Might I suggest a boolean?) if you want the "Processed" in the other table it will get more complicated.

Edit: Apparently mysql doesn't support select into so...

INSERT INTO table2 SELECT field1, field2, field3, "processed" FROM table1

Redfilters code works

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