简体   繁体   English

Mysql 当列匹配值并且不存在时将行复制到另一个表

[英]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.

相关问题 将值从一个表中的列复制到另一个表中的列,其中另一个值与MySQL匹配 - Copy the values from a column in one table to a column in another table where another value matches MySQL 如果另一个表中不存在MySQL,则返回不同的值 - MySQL Return different value if it doesn't exist in another table MYSQL 查询检查 ID 是否存在并将列行值复制到另一个表相关的列名 - MYSQL query check if ID exist and copy column row values to another table related column names 在 MySQL 中,仅当该行不存在时才从同一个表中复制行记录? - In MySQL, Copy a row record from the same table only if that row doesn't exist? mysql:复制行并更改一个值,如果不存在则差一个值 - mysql: copy row & change one value, if doesn't exist with one value difference 当行不存在时,MySQL返回0 - MySQL Return a 0 when row doesn't exist MySQL-从表中选择两列匹配的行。 如果不存在,则选择具有一列的行 - MySQL - Selecting row with two columns match from a table. If doesn't exist select row with one column Cron作业将MySql数据从一列复制到另一列(如果不存在) - Cron job to copy MySql data from one column to another if it doesn't exist 如果表中不存在行,则无法执行MYSQL查询 - MYSQL query doesn't work if row doesn't exist in a table 不存在值时的MySQL SELECT - MySQL SELECT when value doesn't exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM