I have an issue similar to Select incremented integer but I'm trying to take it a step further.
I have a table (a history table that tracks changes to some code) that has, among other things, an ID (id), a modified timestamp (modified), and a foreign key from another table (company_id). If I specify a single company_id, I can get my query to increment a value numbering from 1 to X which order the items happened, but it's only for the specific company_id.
So this...
SELECT id, (@rev := @rev+1) AS revision, company_id
FROM code_history AS ch
INNER JOIN (SELECT @rev := 0) AS r
WHERE company_id = '1'
Returns...
ID | revision | company_id
1 | 1 | 1
3 | 2 | 1
4 | 3 | 1
7 | 4 | 1
9 | 5 | 1
12 | 6 | 1
What I don't know is how to make it so that each unique company_id has it's own incremented list of ID's and revision counts without making a query to the database for each company_id, or even if it's possible.
Data I'd Like...
ID | revision | company_id
1 | 1 | 1
3 | 2 | 1
4 | 3 | 1
7 | 4 | 1
9 | 5 | 1
12 | 6 | 1
2 | 1 | 2
8 | 2 | 2
13 | 3 | 2
18 | 4 | 2
19 | 5 | 2
21 | 6 | 2
5 | 1 | 3
6 | 2 | 3
10 | 3 | 3
11 | 4 | 3
14 | 5 | 3
I appreciate any and all help, thank you!
ch.ID, for consistency.