开发者

In MySQL is it possible to auto increment in pairs?

开发者 https://www.devze.com 2023-02-16 03:22 出处:网络
I have match_id which always comes in pairs.Instead of incrementing as follows: 1,2,3,4... Is it possible to increment like this:

I have match_id which always comes in pairs. Instead of incrementing as follows:

1,2,3,4...

Is it possible to increment like this:

1,1,2,2,3,3,4,4

If not how can I guarantee that I will always have at most two identical match_id. I've tried to implement a Manual solution in code but occasionally I get three at a time if there is a collision.

This is not a primary ke开发者_StackOverflowy. However I want the match_id to come in pairs so that if I look up match 56, it will show the following:

match_id, user_id, score
56, 29, 434
56, 49, 516

Dividing by two is a possible solution although it makes the queries really messy. For example searching for match 56 becomes really convoluted


I'm pretty sure that you cannot autoincrement in pairs like that. However, you can easily treat a column that has 1,2,3,4,... as if it had 1,1,2,2,3,3,4,4,... by using FLOOR((match_id+1)/2) instead of match_id. You can either use that expression in your data retrieval statements or else denormalize your data base and define a column where you insert that value along with the autoincrement value.

0

精彩评论

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