开发者

mysql - satisfy composite primary key while using 'insert into xxx select'

I am importing data to a table structured: content_id|user_id|count - all integers all comprise the composit开发者_StackOverflow中文版e primary key

The table I want to select it from is structured: content_id|user_id

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

insert into new_db.table
select content_id,user_id,xxx from old_db.table

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

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!

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

Any help really appreciated. thanks in advance!

--edit

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

--edit again

12|234
51|45
51|45
51|45
23|67

would ideally insert

12|234|0
51|45|0
51|45|1
51|45|2
23|67|0


INSERT INTO new_db.table (content_id, user_id, cnt)
SELECT old.content_id, old.user_id, COUNT(old.*) - 1 FROM old_db.table old
GROUP BY old.content_id, old.user_id

this would be the way I would go, so if 1 entry it would put 0 on cnt, for more it would just put 1-2-3 etc.

Edit:

Your correct answer would be somewhat complicated but I tested it and it works:

INSERT INTO newtable(user_id,content_id,cnt) 
SELECT o1.user_id, o1.content_id,
    CASE 
        WHEN COALESCE(@rownum, 0) = 0 
        THEN @rownum:=c-1
        ELSE @rownum:=@rownum-1
    END as cnt
FROM 
(SELECT user_id, content_id, COUNT(*) as c FROM oldtable
GROUP BY user_id, content_id ) as grpd 
LEFT JOIN 
(SELECT oldtable.* FROM oldtable) o1 ON 
(o1.user_id = grpd.user_id AND o1.content_id = grpd.content_id)
;


Assuming that in the old db table (source), you will not have the same (content_id, user_id) combination, then you can import using this query

insert newdbtable
select o.content_id, o.user_id, ifnull(max(n.`count`),-1)+1
from olddbtable o
left join newdbtable n on n.content_id=o.content_id and n.user_id=o.user_id
group by o.content_id, o.user_id;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜