简体   繁体   English

使用MySQL,如何在另一个表中不存在该值的情况下插入表中?

[英]With MySQL, how do I insert into a table on condition that the value does not exist in another table?

I have a MySQL database and I would like to insert some values into one table, assuming that a particular value that I am inserting does not match a value in a different table. 我有一个MySQL数据库,我想在一个表中插入一些值,假设我插入的特定值与另一个表中的值不匹配。

Here is a simplified/example structure: 这是一个简化的/示例结构:

Table: invites
    id : int (auto-increment index)
    name : varchar
    message : varchar

Table: donotinvite
    name : varchar (index)

Is it possible to do a conditional insert of a 'name' and 'message' pair into the 'invites' table assuming the 'name' does not match any 'name' from the 'donotinvite' table with a single statement? 假设'name'与'donotinvite'表中的任何'name'与单个语句不匹配,是否可以在'invites'表中有条件地插入'name'和'message'对?

Something like this, perhaps? 也许是这样的事情?

INSERT INTO invites
    SET name = 'joe', message = 'This is an invite'
WHERE NOT EXISTS 
    (SELECT name 
    FROM donotinvite
    WHERE name = 'joe')
INSERT
INTO invites (name, message)
SELECT a.name, a.message
FROM (SELECT @name AS name, @message AS message) a
LEFT JOIN donotinvite d
ON a.name = d.name
WHERE d.name IS NULL

(I'm an MSSQL person, so not sure if all appropriate for MySQL) (我是MSSQL人员,所以不确定是否所有适合MySQL)

INSERT INTO invites
(
    name, message
)
SELECT name = 'joe', message = 'This is an invite'
WHERE NOT EXISTS 
    (SELECT *
    FROM donotinvite
    WHERE name = 'joe')

In practice you might put all the Name / Message in a temporary table and then do: 在实践中,您可以将所有名称/消息放在临时表中,然后执行:

INSERT INTO invites
(
    name, message
)
SELECT T.name, T.message
FROM MyTempTable AS T
WHERE NOT EXISTS 
(
    SELECT *
    FROM donotinvite AS DNI
    WHERE DNI.name = T.name
)
INSERT INTO invites (name, message) 
SELECT 'joe', 'This is an invite' FROM (SELECT 1) t 
WHERE NOT EXISTS (SELECT * FROM donotinvite WHERE name = 'joe');

This would do the job, but I'm not sure it would peform that well. 这样做可以完成这项工作,但我不确定它是否能很好地实现。 It might work for your application: 它可能适用于您的应用程序:

INSERT INTO invites(name, message) 
SELECT p.name, "This is an invite" 
FROM people AS p  
WHERE p.name NOT IN (SELECT name FROM donotinvite);

Do you have a table like people , a catalog of all the people you can send invites to? 你有一个像桌子people ,都可以发送邀请给人们的一个目录?

Here is a re-posting of jonstjohn 's strangely deleted answer, slightly modified by me: 这是jonstjohn奇怪删除的答案的重新发布,我稍微修改过:

INSERT INTO invites (name, message) 
    SELECT 'joe','This is an invite' 
    FROM donotinvite 
    WHERE name <> 'joe'

EDIT: this doesn't do what I want :) . 编辑:这不是我想要的:)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果一个值不存在于另一个表中,如何在其中插入一个值 - How do i insert a value in 1 table if it doesnt exist in another 如何从另一个表中不存在值行的表中选择一行? - How do I select a row from one table where the value row does not exist in another table? 如果不存在值(仅针对某些条件),如何将其插入表中? (MS SQL Server) - How do I insert into a table if a value does not exist, but only for certain criteria? (MS SQL Server) 如何在表中查找值并将其插入到另一个表中? - How do I lookup a value in a table and insert it into another table? 如果值不存在,则将值插入 MySQL 表中? - Insert values into MySQL table if value does not already exist? 仅当数据与Rails中从一个表到另一个表的特定条件匹配时,才如何插入数据? - How do I insert data only if it matches a certain condition from one table to another table in Rails? MySQL:如果另一个表中的值为NULL,如何INS VALUE IN IN TABLE? - MySQL: How to INSERT VALUE INTO TABLE if a value in another table is NULL? 生成另一个表MySQL中不存在的随机值 - Generate a random value that does not exist in another table MySQL 如何 select 一个表中的所有记录在另一个表中不存在于另一个表中的某些条件下? - How to select all records from one table that do not exist in another table for certain condition in another table? 我如何获取一个表中另一个表中不存在的记录? - How do I get a records for a table that do not exist in another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM