0

您好我正在尝试在 postgreSql 表中执行以下操作,但语法有问题。

伪代码:

if (tableA.column1 does not contain value1)
{
    INSERT INTO tableA (column1, column2)
    VALUES value1, value2
}

// do this check even if the above already existed
if (tableB does not contain value1 and value3 in the same row)
{
    // it is ok if value1 and value3 already exist in the table, just not the same row
    INSERT INTO tableB (column1, column2)
    VALUES (value1, value3)
}

return true

对此操作的实际语法的任何帮助将不胜感激!

4

1 回答 1

2
-- first insert:
insert into tablea (col1, col2)
select 1,2
from tablea
where not exists (select * from tablea where col1 = 1);

-- second insert (same logic to apply a conditional insert)
insert into tableb (col1, col2)
select 1,2
from tableb
where not exists (select * from tableb where col1 = 1 and col2 = 2);
于 2013-03-22T14:14:23.557 回答