有一个表其中一个字段是手机号,另一个字段是次数,如果是判断手机号表里没有则insert,如果手机号表里已经存在则update次数列加1,如何用一个sql语句完成呢谢谢!

解决方案 »

  1.   

    insert into t_jiab(mp) values (100)
    ON DUPLICATE KEY UPDATE cnt=cnt+1;mysql> create table t_jiab(
        ->  mp      int primary key,
        ->  cnt     int default 1
        -> );
    Query OK, 0 rows affected (0.14 sec)mysql> insert into t_jiab(mp) values (100)
        -> ON DUPLICATE KEY UPDATE cnt=cnt+1;
    Query OK, 1 row affected (0.06 sec)mysql> select * from t_jiab;
    +-----+------+
    | mp  | cnt  |
    +-----+------+
    | 100 |    1 |
    +-----+------+
    1 row in set (0.00 sec)mysql> insert into t_jiab(mp) values (100)
        -> ON DUPLICATE KEY UPDATE cnt=cnt+1;
    Query OK, 2 rows affected (0.06 sec)mysql> select * from t_jiab;
    +-----+------+
    | mp  | cnt  |
    +-----+------+
    | 100 |    2 |
    +-----+------+
    1 row in set (0.00 sec)mysql>
      

  2.   

    replace into tt set 手机号=你的手机号,次数=次数+1
      

  3.   

    DUPLICATE KEY 是什么意思呢?
      

  4.   


    你的表中的主键或唯一键有冲突的时候,不再执行insert 替代为 update具体可参见
    http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#insert
    13.2.4. INSERT语法
      

  5.   

    如果我不使用主键或唯一键,而是有两个字段比如根据手机号和日期作为条件判断,如果手机号和日期都匹配则update 有一个不匹配则insert,这个怎么办呢
      

  6.   

    如果我不使用主键或唯一键,而是有两个字段比如根据手机号和日期作为条件判断,如果手机号和日期都匹配则update 有一个不匹配则insert,这个怎么办呢这种情况就只有用两条SQL语句解决了
      

  7.   


    很简单,你直接把 (手机号,日期) 做一个复合的唯一键不就行了?
    mysql> create table t_jiab(
        ->  mp      int,
        ->  tdate    date,
        ->  cnt     int default 1
        -> );
    Query OK, 0 rows affected (0.08 sec)mysql>
    mysql> create UNIQUE index uk_t_jiab on t_jiab (mp,tdate);
    Query OK, 0 rows affected (0.09 sec)
    Records: 0  Duplicates: 0  Warnings: 0mysql> insert into t_jiab(mp,tdate) values (100,'2009-06-29')
        -> ON DUPLICATE KEY UPDATE cnt=cnt+1;
    Query OK, 1 row affected (0.06 sec)mysql> select * from t_jiab;
    +------+------------+------+
    | mp   | tdate      | cnt  |
    +------+------------+------+
    |  100 | 2009-06-29 |    1 |
    +------+------------+------+
    1 row in set (0.00 sec)mysql> insert into t_jiab(mp,tdate) values (100,'2009-06-29')
        -> ON DUPLICATE KEY UPDATE cnt=cnt+1;
    Query OK, 2 rows affected (0.06 sec)mysql> select * from t_jiab;
    +------+------------+------+
    | mp   | tdate      | cnt  |
    +------+------------+------+
    |  100 | 2009-06-29 |    2 |
    +------+------------+------+
    1 row in set (0.00 sec)mysql>
      

  8.   

    是的,不过日期这个字段是形如2009-06-30 21:30:12这样的内容,而我传进来作条件的日期是2009-06-30,这样用日期和手机号做联合唯一键肯定就不合适,所以我才想有没有不用他们做联合主键或联合唯一键的sql写法?
      

  9.   

    mysql> create table t_jiab(
        ->  mp      int,
        ->  tdate    date,
        ->  cnt     int default 1
        -> );
    Query OK, 0 rows affected (0.09 sec)mysql> create UNIQUE index uk_t_jiab on t_jiab (mp,tdate);
    Query OK, 0 rows affected (0.11 sec)
    Records: 0  Duplicates: 0  Warnings: 0mysql> insert into t_jiab(mp,tdate)
        -> select 100, ifnull((select tdate from t_jiab where mp=100 and date(tdate)=date('2009-06-29 06:41:14')),'2009-06-29 06:41:14')
        -> ON DUPLICATE KEY UPDATE cnt=cnt+1;
    Query OK, 1 row affected, 1 warning (0.06 sec)
    Records: 1  Duplicates: 0  Warnings: 1mysql> select * from t_jiab;
    +------+------------+------+
    | mp   | tdate      | cnt  |
    +------+------------+------+
    |  100 | 2009-06-29 |    1 |
    +------+------------+------+
    1 row in set (0.00 sec)mysql> insert into t_jiab(mp,tdate)
        -> select 100, ifnull((select tdate from t_jiab where mp=100 and date(tdate)=date('2009-06-29 02:31:13')),'2009-06-29 02:31:13')
        -> ON DUPLICATE KEY UPDATE cnt=cnt+1;
    Query OK, 2 rows affected (0.06 sec)
    Records: 1  Duplicates: 1  Warnings: 0mysql> select * from t_jiab;
    +------+------------+------+
    | mp   | tdate      | cnt  |
    +------+------------+------+
    |  100 | 2009-06-29 |    2 |
    +------+------------+------+
    1 row in set (0.00 sec)mysql> insert into t_jiab(mp,tdate)
        -> select 100, ifnull((select tdate from t_jiab where mp=100 and date(tdate)=date('2009-06-30 02:31:13')),'2009-06-30 02:31:13')
        -> ON DUPLICATE KEY UPDATE cnt=cnt+1;
    Query OK, 1 row affected, 1 warning (0.06 sec)
    Records: 1  Duplicates: 0  Warnings: 1mysql> select * from t_jiab;
    +------+------------+------+
    | mp   | tdate      | cnt  |
    +------+------------+------+
    |  100 | 2009-06-29 |    2 |
    |  100 | 2009-06-30 |    1 |
    +------+------------+------+
    2 rows in set (0.00 sec)mysql>
      

  10.   

    oracle的实现:
    merge into tb_user_session us
    using (select 1 from dual) ustemp
    on (us.account='10102177')
    when matched then
      update set us.sessionid = '123456789'
    when not matched then
      insert (us.account, us.sessionid) values ('10102177', '888888888');本来merge into是用于检索两张表,进行匹配然后处理update还是insert的
    现在结合dual来做单表处理,也照样行
    tb_user_session的字段有: account,sessionid