开发者

How to insert the records in a table with where clause

开发者 https://www.devze.com 2023-03-17 12:57 出处:网络
How to insert the records in a table with where clause I want to insert the values in a column of a same table, by the reference of another column values in same table by using wh开发者_运维技巧ere c

How to insert the records in a table with where clause

I want to insert the values in a column of a same table, by the reference of another column values in same table by using wh开发者_运维技巧ere clause.


Insert into Table_1 (col_a, col_b) 
select val_1, val_2 from dual
where 0 = (select count(*) from Table_1 where col_a = val_1);

This would prevent inserting the value twice.


  1. To copy rows of the same table and specifying value for specific column:

    INSERT INTO payment_tbl (col1, col2, col3) SELECT col1, col2, 'i_changed_value_of_col3' FROM payment_tbl WHERE item_description = 'vegetables'

  2. To duplicate rows:

    INSERT INTO payment_tbl SELECT * FROM payment_tbl WHERE item_description = 'vegetables'

0

精彩评论

暂无评论...
验证码 换一张
取 消