How to add a where clause in a MySQL Insert statement?

前端 未结 8 977
刺人心
刺人心 2020-11-30 12:56

This doesn\'t work:

INSERT INTO users (username, password) VALUES (\"Jack\",\"123\") WHERE id=\'1\';

Any ideas how to narrow insertion to a

相关标签:
8条回答
  • 2020-11-30 13:34
    UPDATE users SET username='&username', password='&password' where id='&id'
    

    This query will ask you to enter the username,password and id dynamically

    0 讨论(0)
  • 2020-11-30 13:41

    A conditional insert for use typically in a MySQL script would be:

    insert into t1(col1,col2,col3,...)
    select val1,val2,val3,...
      from dual
     where [conditional predicate];
    

    You need to use dummy table dual.

    In this example, only the second insert-statement will actually insert data into the table:

    create table t1(col1 int);
    insert into t1(col1) select 1 from dual where 1=0;
    insert into t1(col1) select 2 from dual where 1=1;
    select * from t1;
    +------+
    | col1 |
    +------+
    |    2 |
    +------+
    1 row in set (0.00 sec)
    
    0 讨论(0)
提交回复
热议问题