A table contains something like the following:
ID: 4
DATE: 040910
COUNT: 42
If the ID and DATE already exist in the table, increase the count by one. If the specific ID and DATE do not already exist, create with a count of 1. 'ID' is not unique - it may be duplicated in the table as long as the date code changes.
Everytime I think of a solution, it seems to incur possible race conditions. I thought about BEGIN/END transaction, but that doesn't seem 100% reliable either. I can't just do the insert and if it fails then update unless I merge the ID field into DATE and make it a unique column.
Ideas?
Edit: I've just thought of this:
INSERT INTO table (...) VALUES (...)
WHERE NOT EXISTS (SELECT 1 FROM table WHERE table.id == x AND table.date == y)
UPDATE table SET count = count + 1 WHERE table.id == x AND table.date == y
So the update is always executed, but the insert depends on the current value of the table, but I'm concerned that still 2 records could appear with the same id & date.