gpt4 book ai didi

mysql - 如果值不存在则插入

转载 作者:行者123 更新时间:2023-11-29 01:17:04 24 4
gpt4 key购买 nike

在我的数据库中,我有 tickets_users 将用户关联到工单。

如果还没有这个 users_id(例如用户 1444),我想做一个 INSERT 检查 tickets_users type = 2 WHERE tickets_id = something(例如票号 455);如果它已经存在什么都不做,否则

INSERT INTO tickets_users (tickets_id, users_id, type) VALUES (455, 1444, 2) 
WHERE tickets_id = 455

最佳答案

INSERT INTO tickets_users (tickets_id, users_id, type) 
select 455, 1444, 2 from dual
WHERE not exists (select 1 from tickets_users where type = 2 and tickets_id = something)

试试这个

编辑:
添加多行

INSERT INTO tickets_users (tickets_id, users_id, type) 
select 455, 1444, 2 from dual
WHERE not exists (select 1 from tickets_users where type = 2 and tickets_id = 455)
union all
select 456, 1444, 2 from dual
WHERE not exists (select 1 from tickets_users where type = 2 and tickets_id = 456)

关于mysql - 如果值不存在则插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17829256/

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