Re: Insert data if it is not existing - Mailing list pgsql-general

From tango ward
Subject Re: Insert data if it is not existing
Date
Msg-id CAA6wQLK7c=_S23TUP3H22LTEcmt42tqqpvb6tCTmD0NXEM2ZjA@mail.gmail.com
Whole thread Raw
In response to Re: Insert data if it is not existing  ("David G. Johnston" <david.g.johnston@gmail.com>)
Responses Re: Insert data if it is not existing
List pgsql-general

On Thu, May 24, 2018 at 9:09 AM, David G. Johnston <david.g.johnston@gmail.com> wrote:
On Wednesday, May 23, 2018, tango ward <tangoward15@gmail.com> wrote:


curr.pgsql.execute('''
INSERT INTO my_table(name, age)
SELECT %s, %s
WHERE NOT EXISTS(SELECT name FROM my_table WHERE name= name)
''', ('Scott', 23))

So, WHERE name = name is ALWAYS true and so as long as there is at least one record in my_table the exists returns true, and the not inverts it to false and the main select returns zero rows.  You have successfully inserted a record that doesn't exist (i.e., you've inserted nothing just like you observe).

David J.

Any advice on this Sir? Even adding the FROM statement in SELECT statement doesn't insert the data


INSERT INTO my_table(name, age)
SELECT %s, %s
FROM my_table
WHERE NOT EXISTS(SELECT name from my_table WHERE name = name)''', ('Scott', 23)

I also need to perform the same task but on another table but the data for that is from another DB.

pgsql-general by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: Insert data if it is not existing
Next
From: "David G. Johnston"
Date:
Subject: Re: Insert data if it is not existing