开发者

Reuse Rows in mysql table without auto incrementing

hey let me explain my problem. I have a mysql table in which i store data feeds开发者_StackOverflow社区 from say 5 different sites. Now i update the feeds once daily. I have this primary key FeedId which auto-increments. Now what i do is when i update feeds from particular site i delete previous data for that site from my table and enter the new one. This way the new data is filled in the rows occupied by previous deleted data and if this time there are more feeds rest are entered at the end of table. But the FeedId is incremented for all the new data. What i want is that the feeds stored in old locations retain previous Id n only the extra ones being saved at the end get new incremented Ids. Please help as i cant figure out how to do that.


A better solution would be to set a unique key on the feed (aside from the auto-incremented key). Then use INSERT ON DUPLICATE KEY UPDATE

INSERT INTO feeds (name, url, etc, etc2, `update_count`) 
    VALUES ('name', 'url', 'etc', 'etc2', 1) 
    ON DUPLICATE KEY UPDATE
        `etc` = VALUES(`etc`),
        `etc2` = VALUES(`etc2`),
        `update_count` = `update_count` + 1;

The benefit is that you're not incrementing the ids, and you're still doing it in one atomic query. Plus, you're only updating / changing what you need to change. (Note that I included the update_count column to show how to update a field)...


Marking the post as delete based on the comments Try REPLACE INTO to merge the data. More information @:

http://dev.mysql.com/doc/refman/5.0/en/replace.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜