<?php

include ('r_dbc.php');
$sql = "truncate table ccmanager_tags";
$result = mysqli_query($conn, $sql);


$i = -1;

do {

echo $i;
echo "<br>";
$i--;

$sql1 = "insert ignore into ccmanager_tags (tag) 
select distinct substring_index(substring_index(tags,\",\",".$i."),\",\",1)
from ccmanager";
$result = mysqli_query($conn, $sql1);

echo $sql1;

} while ($i > -20);

?>


<?php

include ('r_dbc.php');
$sql = "truncate table ccmanager_tags_table";
$result = mysqli_query($conn, $sql);


$i = -1;

do {

echo $i;
echo "<br>";
$i--;

$sql2 = "insert ignore into ccmanager_tags_table (mcid,tcid)
select m.cid,t.cid
from ccmanager m, ccmanager_tags t
where substring_index(substring_index(m.tags,\",\",".$i."),\",\",1) = t.tag";
$result = mysqli_query($conn, $sql2);

echo $sql2;

} while ($i > -20);

?>