[英]Mysql copy row to another table when column matches a value AND doesn't exist
I'm a long time lurker but my first post, thanks for the invaluable advice over the years!我是一个很长时间的潜伏者,但我的第一篇文章,感谢多年来的宝贵建议!
I have a bit of a mysql conundrum, I'm designing a form for a venue for uses with WiFi and COVID etc, other stuff.我有一点 mysql 难题,我正在设计一个用于 WiFi 和 COVID 等其他东西的场所的表格。
Now, it's a PHP form which writes into a database, the info is kept for 30 days until it's purged (30 days from the date stamp).现在,它是一个写入数据库的 PHP 表单,信息将保留 30 天,直到它被清除(从日期戳起 30 天)。 This runs on an event, every day (and works fine).
这每天都在一个事件上运行(并且工作正常)。
I wanted to add in a checkbox to say add them to a mailing list so I figured I needed another table to copy the info for the subscribers that obviously doesn't get purged for use later.我想添加一个复选框以将它们添加到邮件列表中,因此我想我需要另一个表来复制订阅者的信息,这些信息显然不会被清除以供以后使用。
Currently I have this query that works:目前我有这个有效的查询:
INSERT INTO table2
SELECT *
FROM table1
WHERE mailing = 'on';
But I really need to copy the row where mailing='on' AND the email address doesn't exist.但我真的需要复制 mailing='on' 和 email 地址不存在的行。 It's this part that I'm really stuck on, is it possible to do this within the same query?
这是我真正坚持的部分,是否可以在同一个查询中执行此操作?
Thanks in advance.提前致谢。
You need in something like你需要类似的东西
INSERT INTO table2 ( {columns list} )
SELECT {columns list}
FROM table1 t1
WHERE mailing='on'
AND NOT EXISTS ( SELECT NULL
FROM table2 t2
WHERE t1.email = t2.email );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.