Q:

How to insert data into table using different conditions

There is one table,  
t1(network,totalCount,partialCount)

The other table is,  
t2(network,isPartial,count)

Example:
T1
network,totalCount,partialCount
  1        100         70
  2        200         130

T2
network,isPartial,Count
   3      Y        78
   3      N        200
   4      Y        150
   4      N        300     

Resulting T1 table after T2 rows are inserted
 network,totalCount,partialCount
  1        100         70
  2        200         130
  3        200         78
  4        300         150

The condition should be:

If t2.isPartial = 'Y' insert the data into t1.partialCount.
If t2.isPartial = 'N' insert the data into t1.totalCount.

So what should I do to make this implemented in SQL or Stored Procedure?
Thanks.

A:

Try this, assuming T1 has primary key (network) and you have finished loading rows into T2 and none of the networks in T2 are in T1:
insert into T1 (network, totalCount, partialCount)
select network,
       sum(case ispartial when 'N' then count else 0 end),
       sum(case ispartial when 'Y' then count else 0 end)
from T2
group by network;

If there exists at least one row in T2 that has a matching T1, then you'll probably have to write a procedure to loop through the query's output because I don't think MySQL's insert command syntax allows for a subquery along with the on duplicate key clause.

