How to count up rows with duplicate id I want to count the number of rows with duplicate ids

My current code is

SET @x=1;

SELECT

c.id,
so.trans_id,
IF(c.id = c.id, @x + 1, @x) as count_t

FROM customer c
LEFT JOIN sales_order so


Current Result

+-----+----------+----------+
| id  | trans_id | count_t  |
+-----+----------+----------+
| 101 | 100001   |  2       |
| 101 | 100059   |  2       |
| 101 | 100061   |  2       |
| 102 | 100030   |  2       |
| 102 | 100035   |  2       |
| 102 | 100090   |  2       |
| 103 | 100005   |  2       |
| 103 | 100115   |  2       |
+-----+----------+----------+


Desired Result

+-----+----------+----------+
| id  | trans_id | count_t  |
+-----+----------+----------+
| 101 | 100001   |  1       |
| 101 | 100059   |  2       |
| 101 | 100061   |  3       |
| 102 | 100030   |  1       |
| 102 | 100035   |  2       |
| 102 | 100090   |  3       |
| 103 | 100005   |  1       |
| 103 | 100115   |  2       |
+-----+----------+----------+


SQL Version:

MySQL 5.6.32-78.1



UPDATE:

I added a nested query and a GROUP BY function to my code but it doesn't seem to count properly.

My NEW code is

SELECT

id,
time,
next_time,
(SELECT COUNT(1) FROM ecms.customer c1
LEFT JOIN ecms.sales_order so1 ON so1.customer_id = c1.id
WHERE c1.id = t.id AND so1.date_created &lt;= t.time) as count_t

FROM(
SELECT

c.id,
so.date_created as time,
(
    SELECT
        MIN(so1.date_created)

    FROM ecms.customer c1
    LEFT JOIN ecms.sales_order so1 ON so1.customer_id = c1.id
    WHERE c1.id = c.id
    AND so1.date_created &gt; so.date_created
    AND IF((TIME_TO_SEC(TIMEDIFF(so1.date_created,
    so.date_created))/360)&gt;=60,1,0)=1
    ) as next_time


    FROM ecms.customer c
    LEFT JOIN ecms.sales_order so ON so.customer_id = c.id
    LEFT JOIN ecms.sales_order_item soi ON soi.sales_order_id = so.id
    LEFT JOIN ecms.product p ON p.id = soi.product_id
    LEFT JOIN ecms.history h ON h.transaction_id = so.transaction_id
    GROUP BY c.id, next_time
) as t

WHERE id = 7941


Sample Result would be

+-----+----------+-----------+----------+
| id   | time    | next_time | count_t  |
+-----+----------+-----------+----------+
| 7941 | 100001  | 100061    |  1       |
| 7941 | 100061  | 100121    |  4       |
| 7941 | 100121  | 100181    |  7       |
| 7941 | 100181  | 100241    |  8       |
| 7941 | 100241  | 100301    |  16      |
| 7941 | 100301  | 100361    |  21      |
| 7941 | 100361  | 100421    |  45      |
| 7941 | 100421  | NULL      |  69      |
+------+---------+-----------+----------+


Sample Result Desired

+-----+----------+-----------+----------+
| id   | time    | next_time | count_t  |
+-----+----------+-----------+----------+
| 7941 | 100001  | 100061    |  1       |
| 7941 | 100061  | 100121    |  2       |
| 7941 | 100121  | 100181    |  3       |
| 7941 | 100181  | 100241    |  4       |
| 7941 | 100241  | 100301    |  5       |
| 7941 | 100301  | 100361    |  6       |
| 7941 | 100361  | 100421    |  7       |
| 7941 | 100421  | NULL      |  8       |
+------+---------+-----------+----------+
Try something like

SELECT @cnt:=CASE WHEN c.id=@id THEN @cnt+1 ELSE 1 END count_t,
       @id:=c.id id, 
       so.trans_id trans_id
FROM (SELECT @id:=0, @cnt:=0) init_vars,
     customer c LEFT JOIN sales_order so ON ...
ORDER BY c.id, so.trans_id;


fiddle