0

Let's say I have a table with following scheme: (Note the Post Number column)

-- UserID  -- Post Id  -- Post Number*

-- 4       -- 85       -- 1
-- 4       -- 86       -- 2
....
-- 5       -- 9362     -- 1
-- 4       -- 9363     -- 3

Is there a way to auto_increment a column independently from all other entries and only respect those with the same primary key? If so, how would someone implement this?

Thanks a lot.

3
  • 1
    You mean auto increment the post number?
    – theMayer
    Commented Feb 9, 2018 at 18:05
  • 1
    I guess the OP wants to increment the Post Number according to UserId column. So basically each UserId will have his own set of PostNumber
    – Paul Karam
    Commented Feb 9, 2018 at 18:10
  • @Paul Karam Yes that is exactly what I need.
    – thelearner
    Commented Feb 9, 2018 at 18:35

2 Answers 2

3

In order to achieve what you're looking for, you have to use triggers. There's no other direct way to accomplish this task (I guess).

I did try a fast demo now:

Create Table SoQuestion (
  UserId int,
  PostId int,
  PostNumber int null
 );

 CREATE TRIGGER inc_post_num 
 BEFORE INSERT ON SoQuestion
 FOR EACH ROW
 set New.PostNumber = (select num 
                       From (select count(*) as num 
                             from SoQuestion 
                             where UserId = New.UserId) as b) 
                     + 1;


insert into SoQuestion (UserId, PostId) Values (1,1);
insert into SoQuestion (UserId, PostId) Values (1,10);
insert into SoQuestion (UserId, PostId) Values (1,20);
insert into SoQuestion (UserId, PostId) Values (2,1);
insert into SoQuestion (UserId, PostId) Values (2,10);
insert into SoQuestion (UserId, PostId) Values (3,1);
insert into SoQuestion (UserId, PostId) Values (4,1);

select * FROM SoQuestion;

And here's the output that I got:

UserId | PostId | PostNumber |
==============================  
1      | 1      | 1          |
1      | 10     | 2          |
1      | 20     | 3          |
2      | 1      | 1          |
2      | 10     | 2          |
3      | 1      | 1          |
4      | 1      | 1          |

Here's the demo.


After going through the Auto_Increment documentation, I found another way to achieve this without using triggers. The idea is about creating an Auto_Increment column and add it with another column as PRIMARY KEY. In our case it would be UserId and the AUTO_INCREMENT would be PostNumber and they both form the primary key. This is how:

Create Table SoQuestion (
  UserId int,
  PostId int,
  PostNumber int NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (UserId, PostNumber)
 );

insert into SoQuestion (UserId, PostId) Values (1,1);
insert into SoQuestion (UserId, PostId) Values (1,10);
insert into SoQuestion (UserId, PostId) Values (1,20);
insert into SoQuestion (UserId, PostId) Values (2,1);
insert into SoQuestion (UserId, PostId) Values (2,10);
insert into SoQuestion (UserId, PostId) Values (3,1);
insert into SoQuestion (UserId, PostId) Values (4,1);

select * FROM SoQuestion;

This would give us the same output that the first way gave:

UserId | PostId | PostNumber |
==============================  
1      | 1      | 1          |
1      | 10     | 2          |
1      | 20     | 3          |
2      | 1      | 1          |
2      | 10     | 2          |
3      | 1      | 1          |
4      | 1      | 1          |

And here's the demo for the second way.

2
  • You saved me. Thanks a lot.
    – thelearner
    Commented Feb 9, 2018 at 19:16
  • Bug... If a row is deleted COUNT(*) causes trouble. Instead use MAX(PostId). But, then it gets messy if there are no entries yet.
    – Rick James
    Commented Feb 20, 2018 at 21:46
2
SET New.PostId := IFNULL(
      ( SELECT  MAX(PostId)+1
            FROM  SoQuestion
            WHERE  UserId = New.UserId ), 1);

This should avoid the bug wherein a DELETE messes with COUNT(*).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.