I have a table called "stats" with:
user varchar(40) not null primary key,
count mediumint not null.

I want to do something like this:

UPDATE stats SET count=count+1 WHERE user='$user';

But if there is no row in the "stats" table having user='$user', that
statement will not create the row.

The following will not work since it is not possible to access the old
values of a row being replaced:

REPLACE INTO stats SET count=count+1, user='$user';

The following will raise an error if the user already exists, causing the
program to stop:

INSERT INTO stats SET user='$user', count=0;
UPDATE stats SET count=count+1 WHERE user='$user';

So far, the only solution I've found is:

SELECT * FROM stats WHERE user='$user';
if $sth->rows then
  INSERT INTO stats SET user='$user', count=1;
else
  UPDATE stats SET count=count+1 WHERE user='$user';

I was wondering, is there a more concise way I can write this query to
achieve the same effect?



---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to