插入选择不存在的位置

时间:2013-02-02 17:19:27

标签: mysql

您好我仍在努力使用以下代码

INSERT INTO m4zbv_mt_cfvalues (cf_id, link_id,value) 
    SELECT 87,link_id,1
    FROM m4zbv_mt_links WHERE Upper(`link_name`) like'%#VMWARE APPLIANCE DEFAULT PASSWORDS%'
    WHERE NOT EXISTS(select cf_id, link_id from m4zbv_mt_cfvalues where cf_id=87 and link_id=m4zbv_mt_links.link_id)

这部分工作正常:

INSERT INTO m4zbv_mt_cfvalues (cf_id, link_id,value) 
    SELECT 87,link_id,1
    FROM m4zbv_mt_links WHERE Upper(`link_name`) like'%#VMWARE APPLIANCE DEFAULT PASSWORDS%'

我不确定如何从第一个选择中传递m4zbv_mt_links.link_id的值以及如何整合WHERE NOT EXISTS

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

应该是and而不是where

INSERT INTO m4zbv_mt_cfvalues (cf_id, link_id,value) 
SELECT 87,link_id,1
FROM m4zbv_mt_links WHERE Upper(`link_name`) like'%#VMWARE APPLIANCE DEFAULT PASSWORDS%'
and NOT EXISTS
               (select cf_id, link_id 
                from m4zbv_mt_cfvalues 
                where cf_id=87 and link_id=m4zbv_mt_links.link_id)

答案 1 :(得分:0)

如果你要做的就是避免重复,只需要INSERT IGNORE(并删除WHERE NOT EXISTS子句)