MySQL-如果不存在,则使用subQuery进行插入

时间:2018-10-08 14:42:34

标签: mysql sql

我很困惑,SQL不是我的强项。

我一直在寻找以下答案,但我可以使用自己的查询:

INSERT INTO table_listnames (name, address, tele)
SELECT * FROM (SELECT 'Rupert', 'Somewhere', '022') AS tmp
WHERE NOT EXISTS (
    SELECT name FROM table_listnames WHERE name = 'Rupert'
) LIMIT 1;

AND

INSERT INTO `table` (value1, value2) 
SELECT 'stuff for value1', 'stuff for value2' FROM DUAL
WHERE NOT EXISTS (SELECT * FROM `table` 
      WHERE value1='stuff for value1' AND value2='stuff for value2') 
LIMIT 1 

我想将子查询的结果插入新表(匹配项),这是我的查询:

INSERT INTO matches (fk_object_id, object_adress, fk_lookout_id, lookout_name)
    (SELECT o.id as oid, o.adress as oa, l.id as lid, l.first_name as lfn 
    FROM geo_lookout gl
    JOIN geo_object go ON go.`fk_geo_id` = gl.`fk_geo_id`
    JOIN object o ON o.id = go.`fk_object_id`
    JOIN attri_object ao ON ao.`fk_object_id` = go.`fk_object_id`
    JOIN attri_lookout al ON al.`fk_attri_id` = ao.`fk_attri_id`
    JOIN lookout l ON l.`id` = al.`fk_lookout_id`
    WHERE o.`have_size` <= l.`max_size`
    AND o.`have_size` >= l.`min_size`
    GROUP BY o.id)
WHERE NOT EXISTS (SELECT * FROM matches WHERE fk_object_id = oid AND fk_lookout_id = lid)
LIMIT 1

我总是出现以下错误:

  

您的SQL语法有错误;检查手册   对应于您的MariaDB服务器版本,以使用正确的语法   靠近'WHERE NOT EXISTS(SELECT * FROM匹配WHERE fk_object_id = oid   AND fk_lookout_'在第12行

具有所有JOINS的大型SELECT查询本身可以正常工作:

oid        oa          lid         lfn
45    aGoodStreet       32    Andrew Phillis
44    aGoodStreet       32    Andrew Phillis

你们看到的比我清楚吗?可能是:)

亲切的问候

2 个答案:

答案 0 :(得分:0)

为什么您的select周围有括号?想必您打算:

INSERT INTO matches (fk_object_id, object_adress, fk_lookout_id, lookout_name)
    SELECT o.id as oid, o.adress as oa, l.id as lid, l.first_name as lfn 
    FROM geo_lookout gl
    JOIN geo_object go ON go.`fk_geo_id` = gl.`fk_geo_id`
    JOIN object o ON o.id = go.`fk_object_id`
    JOIN attri_object ao ON ao.`fk_object_id` = go.`fk_object_id`
    JOIN attri_lookout al ON al.`fk_attri_id` = ao.`fk_attri_id`
    JOIN lookout l ON l.`id` = al.`fk_lookout_id`
    WHERE o.`have_size` <= l.`max_size` AND
          o.`have_size` >= l.`min_size` AND
          NOT EXISTS (SELECT * FROM matches m WHERE m.fk_object_id = o.oid AND m.fk_lookout_id = l.lid)
    GROUP BY o.id
    LIMIT 1;

请注意,我不得不在子查询中猜测列的来源。限定所有列引用。

答案 1 :(得分:0)

感谢@Gordon Linoff,查询现在可以正常工作!

非常感谢!

这是有效的查询:

INSERT INTO matches (fk_object_id, object_adress, fk_lookout_id, lookout_name)
    SELECT o.id as oid, o.adress as oa, l.id as lid, l.first_name as lfn 
    FROM geo_lookout gl
    JOIN geo_object go ON go.`fk_geo_id` = gl.`fk_geo_id`
    JOIN object o ON o.id = go.`fk_object_id`
    JOIN attri_object ao ON ao.`fk_object_id` = go.`fk_object_id`
    JOIN attri_lookout al ON al.`fk_attri_id` = ao.`fk_attri_id`
    JOIN lookout l ON l.`id` = al.`fk_lookout_id`
    WHERE o.`have_size` <= l.`max_size` AND
          o.`have_size` >= l.`min_size` AND
          NOT EXISTS (SELECT * FROM matches m JOIN object ON object.id = m.`fk_object_id` JOIN lookout ON lookout.id = m.`fk_lookout_id` WHERE m.fk_object_id = object.id AND m.fk_lookout_id = lookout.id)
    GROUP BY o.id;