INSERT OR UPDATE?

From: smorrey(at)gmail(dot)com
To: pgsql-general(at)postgresql(dot)org
Subject: INSERT OR UPDATE?
Date: 2005-10-09 06:15:37
Message-ID: 1128838537.817935.78430@g44g2000cwa.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello all,

I am writing an app in PHP that uses a PostGres database.
One thing i have noticed is that what should/could be a single line of
SQL code takes about 6 lines of PHP. This seem wasteful and redundant
to me.

Here is a sample of what I'm talking about ($db is a PDO already
defined and created).

$query[1] = "UPDATE my.table, SET somefield = '$someval' WHERE
somecondition";
$query[2] = "INSERT INTO my.table (somefield) VALUES ('$someval')";
if(!$db->query($query[1])){
$db->query($query[2]);
}

What I'm curious to know is if there is some way to simplify this,
either buy some PHP builtin or extension, or possibly something in SQL
I am missing. It seems to me that "UPDATE OR INSERT", should be valid,
but I can't seem to find anything relevant at all about it.

Anyways I hope you don't mind, but I'm crossposting this to
pgsql.general and comp.lang.php to see if I can get some information on
the subject.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Oliver Elphick 2005-10-09 06:24:51 Re: brain-teaser with CONSTRAINT - any SQL experts?
Previous Message Uwe C. Schroeder 2005-10-09 05:59:47 Re: brain-teaser with CONSTRAINT - any SQL experts?