0

I have this data set in MySql:

   p_id  c_id     amount
    5      1      2000
    6      1      3000
    7      1      4000

I need to insert the same data in that table, and just increment the counter of c_id from 1 till 7.

So, in all there will be 21 entries total.

How can I do this with just SQL??

Result:

   p_id  c_id     amount
    5      1      2000
    6      1      3000
    7      1      4000
    5      2      2000
    6      2      3000
    7      2      4000
    5      3      2000
    6      3      3000
    7      3      4000
    5      4      2000
    6      4      3000
    7      4      4000

c_id can go till 9.

nirvair
  • 4,001
  • 10
  • 51
  • 85

2 Answers2

0

Is this what you want?

insert into t(p_id, c_id, amount)
    select t.p_id, c.c_id, t.amount
    from t cross join
         (select 2 as c_id union all select 3 union all select 4 union all
          select 5 union all select 6 union all select 7
         ) c;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
-1

What you need is a sequence generator: generate an integer sequence in MySQL

Use any table with 7 or more rows.

INSERT IGNORE INTO original_table
SELECT o.p_id, n.c_id, o.amount
FROM original_table o
JOIN (
  SELECT @row := @row + 1 as c_id
  FROM some_table t, (SELECT @row := 0) r
  LIMIT 7
) n
  ON true
Community
  • 1
  • 1
slbteam08
  • 681
  • 3
  • 11