2

I'm trying to insert or update a record into a sqlite database, and only update the value if the new value is greater than the old.

The schema is:

 CREATE table IF NOT EXISTS SearchTable
   (Owner INTEGER PRIMARY KEY, Generations INTEGER DEFAULT 0)

I've tried commands like this:

INSERT OR REPLACE INTO SearchTable(Owner, Generations)
 VALUES (?, MAX((SELECT Generations FROM SearchTable WHERE Owner = ?), ?))

but it gives me a null value for the Generations.

I could use some help.

HalR
  • 11,411
  • 5
  • 48
  • 80

1 Answers1

7

Try

INSERT OR REPLACE INTO SearchTable(Owner, Generations)
SELECT ?, MAX(Generations) FROM SearchTable WHERE Owner = ?
peterm
  • 91,357
  • 15
  • 148
  • 157
  • Thanks peterm. I ended up using: INSERT OR REPLACE INTO SearchTable(Owner, Generations) SELECT ?, MIN(?, COALESCE(Generations, 999)) FROM SearchTable WHERE Owner = ? – HalR Mar 21 '13 at 05:33