简体   繁体   中英

Insert same rows but with a specific condition

first of all, sorry for my english grammar, not my native language.

So, i want to insert data to a table. But the condition is that only one column should be different when is needed. To be clearer:

This is my table af_descnom

cod   cdnom  coddesc  monto       stad  
------  ------  -------  --------  --------
246919  130394  D17      150.00           1
246920  130394  D1       30.00            1
246921  130394  D23      46.00            1
246922  130395  D17      150.00           1
246923  130395  D1       30.00            1
246924  130395  D23      135.00           1
246925  130395  D19      350.00           1
246926  130396  D17      150.00           1
246927  130396  D1       30.00            1
246928  130396  D23      135.00           1
246929  130397  D5       999.00           1
246930  130397  D1       30.00            1
246931  130397  D17      150.00           1
246932  130397  D23      40.00            1

How you can see, sometimes the value in the column cdnom is the same. This is the foreign key that save the primary key from my other table af_dnom . What i want to do? Insert all the same values, except cod (is my PK, have auto_increment) and cdnom. Should something like:

cod   cdnom  coddesc  monto       stad  
------  ------  -------  --------  --------
246934  130398  D17      150.00           1
246935  130398  D1       30.00            1
246936  130398  D23      46.00            1
246937  130399  D17      150.00           1
246938  130399  D1       30.00            1
246939  130399  D23      135.00           1
246940  130399  D19      350.00           1

I dont know if this kind of things is possible, i have try everything. Maybe should i typing all?

Is this what you want?

insert into af_descnom(cdnom, coddesc, monto, stad)
    select 130398, coddesc, monto, stad
    from af_descnom
    where cdnom = 130394;

insert into af_descnom(cdnom, coddesc, monto, stad)
    select 130399, coddesc, monto, stad
    from af_descnom
    where cdnom = 130395;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM