doucheng1891
doucheng1891
2011-11-30 11:00

使用php和mysql从多个表复制行

  • php
  • mysql
  • pdo

I'm building a copy campaign function for an app and the way I did it was using loops to go through all the selected rows with php, get their IDs and loop again to insert new rows with same data but with one changed column value. This is my current approach which works perfectly:

<?php
    $id = Url::find('id');
    //Copy campaign row
    $cid = $this->Db->Query("
        INSERT INTO campaigns (`user_id`, `tid`, `featured`, `opt`, `title`, `uri`, `cpi`, `payout`, `cat`, `domain`, `created`, `disabled`, `completed`, `featured_pos`)
        SELECT `user_id`, `tid`, `featured`, `opt`, CONCAT(`title`,' - Copy'), `uri`, `cpi`, `payout`, `cat`, `domain`, `created`, `disabled`, `completed`, `featured_pos` FROM campaigns WHERE id = :id
    ",array(':id' => array(DB::INT => $id)),true);

    //Copy variants
    $vars = $this->Db->selectAll(array('cid' => $id), 'variations');
    if(!empty($vars)) {
        for($i = 0; $i < count($vars); $i++) {
            $old[] = $vars[$i]['id']; //Copy source
            $vid[] = $this->Db->Query("
                INSERT INTO variations (`ordering`, `disabled`, `cid`, `height`, `width`, `bgcolor`, `head`,`title`, `keywords`, `description`, `link`)
                SELECT `ordering`, `disabled`, :cid, `height`, `width`, `bgcolor`, `head`, `title`, `keywords`, `description`, `link` FROM variations WHERE id = :id
                ",array(
                    ':id' => array(DB::INT => $vars[$i]['id']),
                    ':cid' => array(DB::INT => $cid)
                ),true);
        }
    }
    //Copy elements
    if(isset($vid)) {
        for($i = 0; $i < count($vid); $i++) {
            $this->Db->Query("
                INSERT INTO elements (`name`, `var`, `type`, `left`, `top`, `width`, `height`, `z-index`, `html`, `css`)
                SELECT `name`, :vid, `type`, `left`, `top`, `width`, `height`, `z-index`, `html`, `css` FROM elements WHERE var = :id
                ",array(
                    ':vid' => array(DB::INT => $vid[$i]),
                    ':id' => array(DB::INT => $old[$i])
                ),true);
        }
    }
    echo "OK";
    ?>

However I'm sure this isn't the best approach as it executes way too many queries. I've upgraded the script, but I can't figure out how to copy rows to the elements table with new IDs of new variations. The new script:

$id = Url::find('id');
$cid = $this->Db->Query("
    INSERT INTO campaigns (`user_id`, `featured`, `title`, `domain`, `created`, `disabled`, `featured_pos`)
    SELECT :user, `featured`, CONCAT(`title`,' - Copy'), `domain`, `created`, `disabled`, `featured_pos` FROM campaigns WHERE id = :id
",array(':id' => array(DB::INT => $id), ':user' => array(DB::INT => $this->session->user['id'])),true);

//Copy variants

$this->Db->Query("
    INSERT INTO variations (`ordering`, `disabled`, `cid`, `height`, `width`, `bgcolor`, `head`,`title`, `keywords`, `description`, `link`)
    SELECT `ordering`, `disabled`, :id, `height`, `width`, `bgcolor`, `head`, `title`, `keywords`, `description`, `link` FROM variations WHERE cid = :oid
",array(
    ':id' => array(DB::INT => $cid), //new id
    ':oid' => array(DB::INT => $id) //old id
 ),true);

 $this->Db->Query("
    INSERT INTO elements (`name`, `var`, `html`, `type`)
    SELECT e.name, v.id, e.html, e.type FROM elements AS e, variations AS v
    WHERE v.cid = :cid AND e.var = v.id
",array(
    ':cid' => array(DB::INT => $id)
 ),true);

The first 2 queries work as expected, however the last one (INSERT INTO elements) doesn't . That's because it selects the variations from the old campaign, and inserts into var column the old ids instead of previously inserted new ones. Obviously that's because the query is built this way but how do I get the IDs from the new variations without a loop to insert them into elements. (Or am I paranoid too much and my initial approach is fine?)

To be more clear with my question - how do I do this without a php loop:

    INSERT INTO elements (`name`, `var`, `html`, `type`)
    SELECT e.name, (new v.id from the previous query and not the same one as in WHERE clause), e.html, e.type FROM elements AS e, variations AS v
    WHERE v.cid = :cid AND e.var = v.id

Sorry for my grammar mistakes (I just got to work and need a coffee)

Thanks a lot :)

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答