Skip to content
Advertisement

MYSQL Query Mass Pump

$sql = "INSERT INTO `coupon` SET 
                    `name` = 'FREECOUPON', 
                    `coupon_code` = '" . $this->db->escape($couponCode) . "', 
                    `discount` = '38', 
                    `uses_total` = '22', 
                    `status` = '1'
                ";

                $this->db->query($sql);

                $coupon_id = $this->db->getLastId();

                $this->db->query("INSERT INTO coupon_cat SET coupon_id = '" . (int)$coupon_id . "', category_id = 79  ");

The query works, but I want to modify this

$this->db->query("INSERT INTO coupon_cat SET coupon_id = '" . (int)$coupon_id . "', category_id = 79  ");

I want to “mass pump” data into coupon_cat. I need to pump from category 30-180, but in different lines. Any idea how to do it ?

Advertisement

Answer

If I understand you clear you want to insert the same coupon_id to 150 category ids. You can insert all the rows in one query using loop to create all the values.

$coupon_id = (int)$this->db->getLastId();

$values = [];
for ($category_id = 30; $category_id <= 180; $category_id++) {
    $values[] = '(' . $coupon_id . ',' . $category_id . ')';
}

$this->db->query('INSERT INTO `coupon_cat` (coupon_id, category_id) VALUES ' . implode(',', $values));
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement