Note that there are some explanatory texts on larger screens.

plurals
  1. POCopying rows from multiple tables with php and mysql
    primarykey
    data
    text
    <p>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:</p> <pre><code>&lt;?php $id = Url::find('id'); //Copy campaign row $cid = $this-&gt;Db-&gt;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' =&gt; array(DB::INT =&gt; $id)),true); //Copy variants $vars = $this-&gt;Db-&gt;selectAll(array('cid' =&gt; $id), 'variations'); if(!empty($vars)) { for($i = 0; $i &lt; count($vars); $i++) { $old[] = $vars[$i]['id']; //Copy source $vid[] = $this-&gt;Db-&gt;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' =&gt; array(DB::INT =&gt; $vars[$i]['id']), ':cid' =&gt; array(DB::INT =&gt; $cid) ),true); } } //Copy elements if(isset($vid)) { for($i = 0; $i &lt; count($vid); $i++) { $this-&gt;Db-&gt;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' =&gt; array(DB::INT =&gt; $vid[$i]), ':id' =&gt; array(DB::INT =&gt; $old[$i]) ),true); } } echo "OK"; ?&gt; </code></pre> <p>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:</p> <pre><code>$id = Url::find('id'); $cid = $this-&gt;Db-&gt;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' =&gt; array(DB::INT =&gt; $id), ':user' =&gt; array(DB::INT =&gt; $this-&gt;session-&gt;user['id'])),true); //Copy variants $this-&gt;Db-&gt;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' =&gt; array(DB::INT =&gt; $cid), //new id ':oid' =&gt; array(DB::INT =&gt; $id) //old id ),true); $this-&gt;Db-&gt;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' =&gt; array(DB::INT =&gt; $id) ),true); </code></pre> <p>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?)</p> <p>To be more clear with my question - how do I do this without a php loop:</p> <pre><code> 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 </code></pre> <p>Sorry for my grammar mistakes (I just got to work and need a coffee)</p> <p>Thanks a lot :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload