Answer the question
In order to leave comments, you need to log in
How to auto-number fields in MySql?
Fields id(int) | user_id(int)
How to make the id field numbered in order, depending on the user_id
Example
id | user_id
_____________
1 | 1
2 | 1
3 | 1
1 | 2
2 | 2
4 | 1
3 | 2
1 | 3
Answer the question
In order to leave comments, you need to log in
In the manual:
https://dev.mysql.com/doc/refman/5.7/en/example-au...
There is an example similar to yours:
CREATE TABLE animals (
grp ENUM('fish','mammal','bird') NOT NULL,
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (grp,id)
) ENGINE=MyISAM;
INSERT INTO animals (grp,name) VALUES
('mammal','dog'),('mammal','cat'),
('bird','penguin'),('fish','lax'),('mammal','whale'),
('bird','ostrich');
SELECT * FROM animals ORDER BY grp,id;
+--------+----+---------+
| grp | id | name |
+--------+----+---------+
| fish | 1 | lax |
| mammal | 1 | dog |
| mammal | 2 | cat |
| mammal | 3 | whale |
| bird | 1 | penguin |
| bird | 2 | ostrich |
+--------+----+---------+
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question