pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: tango ward <tangoward15@gmail.com>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Insert data if it is not existing
Date: Wed, 23 May 2018 17:04:37 -0700
Message-ID: <a4994636-9c3d-65e1-0e3a-512a377e367c@aklaver.com> (raw)
In-Reply-To: <CAA6wQLKH+djOs9xVFWB5pG9DN_nD8dQrrdOQtB0=8NdVy93KYA@mail.gmail.com>
References: <CAA6wQL+TssWQBKuysQWwOK6ggZ5niLiRXhHbZt8hZTC+qsfwXQ@mail.gmail.com>
	<CAKFQuwbovigqheUFx04dr41+H6DbNsRW_meB-w072SFeAPj7vg@mail.gmail.com>
	<0a76f762-88d8-75a7-de3c-d76094638fdd@aklaver.com>
	<CAA6wQLKH+djOs9xVFWB5pG9DN_nD8dQrrdOQtB0=8NdVy93KYA@mail.gmail.com>

On 05/23/2018 04:58 PM, tango ward wrote:
> Thanks masters for responding again.
> 
> I've tried running the code:
> 
> INSERT INTO my_table(name, age)
> SELECT name, age
> WHERE NOT EXISTS(SELECT name FROM my_table WHERE name= name)

The first thing I see is that:

SELECT name, age

is not being selected from anywhere, for example:

SELECT name, age FROM some_table.

The second thing I see is why not use ON CONFLICT?

> 
> 
> this doesn't give me error but it doesn't insert data either.
> 
> On Thu, May 24, 2018 at 3:35 AM, Adrian Klaver 
> <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> wrote:
> 
>     On 05/23/2018 10:00 AM, David G. Johnston wrote:
> 
>         On Wednesday, May 23, 2018, tango ward <tangoward15@gmail.com
>         <mailto:tangoward15@gmail.com> <mailto:tangoward15@gmail.com
>         <mailto:tangoward15@gmail.com>>> wrote:
> 
>              I just want to ask if it's possible to insert data if it's not
>              existing yet.
> 
> 
>         This seems more like a philosophical question than a technical
>         one...
>         ​but the answer is yes:
> 
>         CREATE TABLE test_t (a varchar, b varchar, c integer);
>         INSERT INTO test_t
>         SELECT '1', '2', 3 WHERE false;​ --where false causes the data
>         to effectively "not exist"
> 
>         As for ON CONFLICT: conflicts can only happen between things
>         that exist.
> 
> 
>     Well that made my day:)
> 
> 
>         David J.
> 
> 
> 
>     -- 
>     Adrian Klaver
>     adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (33+ messages)  latest in thread

Message-ID: <a4994636-9c3d-65e1-0e3a-512a377e367c@aklaver.com>
Permalink:  ../a4994636-9c3d-65e1-0e3a-512a377e367c@aklaver.com/
Also on:    postgresql.org/message-id/a4994636-9c3d-65e1-0e3a-512a377e367c@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, tangoward15@gmail.com, david.g.johnston@gmail.com
  Subject: Re: Insert data if it is not existing
  In-Reply-To: <a4994636-9c3d-65e1-0e3a-512a377e367c@aklaver.com>
  References: <CAA6wQL+TssWQBKuysQWwOK6ggZ5niLiRXhHbZt8hZTC+qsfwXQ@mail.gmail.com> <CAKFQuwbovigqheUFx04dr41+H6DbNsRW_meB-w072SFeAPj7vg@mail.gmail.com> <0a76f762-88d8-75a7-de3c-d76094638fdd@aklaver.com> <CAA6wQLKH+djOs9xVFWB5pG9DN_nD8dQrrdOQtB0=8NdVy93KYA@mail.gmail.com> <a4994636-9c3d-65e1-0e3a-512a377e367c@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox