gpt4 book ai didi

mysql - 使用 SELECT 和 ON DUPLICATE KEY UPDATE 插入 PDO

转载 作者:行者123 更新时间:2023-11-29 02:58:47 24 4
gpt4 key购买 nike

我成功地使用了以下查询,只有在 data_advertentie 中存在具有 ID 的行时才会执行此查询。

INSERT INTO data_contactgegevens (ID_advertentie, telefoonnummer, website)
SELECT ID_advertentie, :telefoonnummer, :website FROM data_advertenties
WHERE ID_advertentie = :ID_advertentie AND unieke_hash_plaatsen = :hash_plaatsen

现在我想添加一个 ON DUPLICATE KEY,因此如果该行存在于 data_contactgegevens 中,该行将被更新。

请帮忙....

最佳答案

insert 语句的末尾添加 on duplicate key update 部分。

示例

create table a(
id int not null auto_increment primary key,
x varchar(10)
);

create table b(
id int not null auto_increment primary key,
y varchar(10)
);

insert into a(x) values ('a'), ('b'), ('c');

insert into b(y) values ('e'), ('d');

insert into a
select * from b
on duplicate key update x = y;

结果:

select * from a;

| ID | X |
|----|---|
| 1 | e |
| 2 | d |
| 3 | c |

这是 SQL fiddle对于这个例子。

关于mysql - 使用 SELECT 和 ON DUPLICATE KEY UPDATE 插入 PDO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27069184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com