Note that there are some explanatory texts on larger screens.

plurals
  1. POmysql - satisfy composite primary key while using 'insert into xxx select'
    primarykey
    data
    text
    <p>I am importing data to a table structured: content_id|user_id|count - all integers all comprise the composite primary key</p> <p>The table I want to select it from is structured: content_id|user_id</p> <p>For reasons quite specific to my use case, I will need to fire quite a lot of data into this regularly enough to want a pure MySQL solution</p> <pre><code>insert into new_db.table select content_id,user_id,xxx from old_db.table </code></pre> <p>I want each row to go in with xxx set to 0, unless this would create a duplicate key, in which case I wish to increment the number, for the current user_id/content_id combination</p> <p>Not being a MySQL expert, I tried a few options like trying to populate xxx by selecting from the target table during insert, with no luck. Also tried using ON DUPLICATE KEY to increment counters instead of the usual UPDATE. But it all seemed a bit daft so I thought I would come here!</p> <p>Any ideas anyone? I have a backup option of wrapping this in PHP, but it would drastically raise the overall running time of the script in which this would be the only non-pure MySQL part</p> <p>Any help really appreciated. thanks in advance!</p> <p>--edit</p> <p>this may sound really awful in principle. but id settle for a way to do it in an update after entering random numbers (i have sent in random numbers to allow me to continue other work at the moment) - and this is a purely dev setup</p> <p>--edit again</p> <pre><code>12|234 51|45 51|45 51|45 23|67 </code></pre> <p>would ideally insert</p> <pre><code>12|234|0 51|45|0 51|45|1 51|45|2 23|67|0 </code></pre>
    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