"Juan E Suris" <[EMAIL PROTECTED]> wrote:
> I need to do something similar to replace, but can't figure out how to do it.
>
> Here's my table:
> user  varchar(15) PRI
> cnt  int(11)
> 
> I want to increment cnt for user if it exists, else insert a row with cnt=1. I tried 
> the following, but mysql complains that I can't use the same table in the update and 
> select:
> 
> replace into table set user='someuser', cnt=(select cnt+1 from table where 
> user='someuser')
> 
> Can this be done (in 1 query, of course)?

 From v4.1.0 MySQL supports INSERT .. ON DUPLICATE KEY UPDATE and you can write your 
query as

        INSERT INTO table VALUES('someuser', 1)
        ON DUPLICATE KEY UPDATE cnt=cnt+1;

        http://www.mysql.com/doc/en/INSERT.html


-- 
For technical support contracts, goto https://order.mysql.com/?ref=ensita
This email is sponsored by Ensita.net http://www.ensita.net/
   __  ___     ___ ____  __
  /  |/  /_ __/ __/ __ \/ /    Victoria Reznichenko
 / /|_/ / // /\ \/ /_/ / /__   [EMAIL PROTECTED]
/_/  /_/\_, /___/\___\_\___/   MySQL AB / Ensita.net
       <___/   www.mysql.com





-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to