1

我知道我应该展示我尝试过的东西,以便获得更好的帮助,但我什至不知道从哪里开始,所以没有什么可展示的。我做得很好,可以把它变成一个聪明的问题。我正在使用 PHP 和 MySQL:

arpr_customs (`id`, `customfield_id`, `contact_id`, `stamp_create`, `stamp_update`, `field_value`)
arpr_contacts (`id`, `stamp_create`, `stamp_update`, `key`, `email_address`, `title`, `first_name`, `middle_name`, `last_name`, `full_name`, `format_preference`, `company`, `department`, `address_1`, `address_2`, `address_3`, `city`, `state`, `postal_code`, `country`, `alternative_email_address_1`, `alternative_email_address_2`, `alternative_email_address_3`, `phone_number_1`, `phone_number_2`, `phone_number_3`, `mobile_phone_number_1`, `mobile_phone_number_2`, `mobile_phone_number_3`, `fax_number_1`, `fax_number_2`, `fax_number_3`, `referer_id`, `schedule_pid`, `export_pid`, `import_subscribe_ip_address`, `import_subscribe_date_time`, `import_confirm_ip_address`, `import_confirm_date_time`, `import_customs`, `import_confirmed`)

鉴于这两个表,我需要识别在 arpr_customs 中没有包含该contact_id 和customfield_id '3' 的行的contact_ids。一旦确定,我需要在 arpr_customs 中为每个允许“id”自动递增的行插入一个新行,customfield_id 设置为“3”,“contact_id”设置为找到的 id。并且 field_value 设置为“Susie”

(Susie 代表将拥有所有当前未拥有帐户的销售人员的姓名。)(customfield_id "3" 是销售人员姓名。)

编辑: 我会把它放在评论中,但它太大了。我接受了斯宾塞的建议,在一张重复的桌子上工作。这实际上对我有用。谢谢@spencer

INSERT INTO testing_customs (`contact_id`,`stamp_create`,`stamp_update`,`customfield_id`,`field_value`) 
SELECT o.id AS contact_id, 1341602090 as stamp_create, 1341602090 as stamp_update, 3 AS customfield_id,'Susie' AS field_value
FROM arpr_contacts o LEFT JOIN testing_customs u ON u.contact_id = o.id
AND u.customfield_id = 3  WHERE u.id IS NULL 
4

4 回答 4

1

在 arpr_customs 表中查找没有“匹配”行的 arpr_contacts 行(假设这里arpr_customs.contact_id是指向arpr_contacts.id

SELECT o.id
  FROM arpr_contacts o
  LEFT 
  JOIN arpr_customs u
    ON u.contact_id = o.id
       AND u.customfield_id = 3
 WHERE u.id IS NULL

这是一个熟悉的“反加入”模式。基本上,该语句是说从联系人中获取所有行,以及从海关中获取所有匹配的行......然后丢弃任何匹配的行,所以我们留下了来自联系人的行,但没有匹配。(我们依赖于在海关表中有一个具有 NOT NULL 约束的列,所以我们可以测试我们是否有匹配项。我在这里假设该id列是主键,我们知道它永远不可能null。(注意:这个查询的实际执行计划与我描述的有点不同,但从概念上讲,这是正在发生的事情的最终结果。)

作为(通常)较慢的替代方案,您还可以使用相关子查询来获得相同的结果:

SELECT o.id
  FROM arpr_contacts o
 WHERE NOT EXISTS (SELECT 1 FROM arpr_customs u 
                    WHERE u.contact_id = o.id 
                      AND u.customfield_id = 3)

或者,如果您要在非常大的表上获得可怕的性能,您可以使用 NOT IN 谓词。(这里要小心避免在列表中获得任何 NULL 值。)

SELECT o.id
  FROM arpr_contacts o
 WHERE o.id NOT IN 
       ( SELECT u.contact_id 
           FROM arpr_customs u
           WHERE u.contact_id IS NOT NULL
             AND u.customfield_id = 3
       )

这只是部分回答了你的问题。

customfield_id我现在注意到,正如我正在审查的那样,我在该列上遗漏了任何谓词。固定的

它没有解决插入“缺失”行的问题......

您可以获取 arpr_contacts 表中所需的任何列,然后为其他列提供值,以执行 INSERT ... SELECT ...

我建议您只运行 SELECT 部分,然后(如果您不确定)创建一个临时表作为接受插入的替代。(您可以从 a 获取输出SHOW CREATE TABLE arpr_customs并对其进行编辑(删除外键约束,并更改表名,并将其用作插入的目标。)

INSERT INTO arpr_customs (`contact_id`,`customfield_id`,`field_value`)
SELECT o.id    AS contact_id
     , 3       AS customfield_id
     , 'Susie' AS field_value
  FROM arpr_contacts o
  LEFT
  JOIN arpr_customs u
    ON u.contact_id = o.id
       AND u.customfield_id = 3
 WHERE u.id IS NULL

如果idarpr_customs 表中的列定义为 AUTO_INCREMENT,则会自动为其分配一个值。

于 2012-07-06T21:22:17.677 回答
0

not exist您可以使用、not inleft join+is null查询来做您想做的事情。

所以像:

SELECT * FROM arpr_contacts
WHERE id NOT IN (SELECT contact_id FROM arpr_customs)

我不确定确切的 MySQL 语法。

另请访问此链接以了解 Not Exist/Not In/Left Join 之间的差异。

于 2012-07-06T21:21:55.287 回答
0
INSERT INTO arpr_customs (customfield_id,contact_id,field_value)
SELECT '3',arpr_contacts.id,'Susie'
FROM arpr_contacts LEFT JOIN arpr_customs
on arpr_customs.contact_id=apr_contacts.id
where arpr_customs.contact_id is null
于 2012-07-06T21:23:49.143 回答
0

尝试类似的东西

Insert arpr_customs(Customfield_id,contact_id,field_value)
Select '3',arpr_contacts.id,'Susie'
From arpr_contacts
outer join arpr_customs On arpr_customs.customfield_id = '3' 
          and arpr_customs.contact_id = arpr_contacts.id
where arpr_customs.contact_id is null

基本上它是'3'类型的联系人和海关的外部连接,其中过滤掉那些有匹配记录的地方,然后将它们用于插入。

不过,我的 Mysql 并不出色,因此您可能在其中的语法很有趣。

于 2012-07-06T21:23:56.580 回答