i have a table with fields
Table A)
When i run sql command
SELECT id,
user,
`low_id`
FROM ( SELECT @r:= IF(@u = user, @r + 1,1) AS `low_id`,
id,
user,
@u:= user
FROM usertest,
(SELECT @i:= 1) AS r,
(SELECT @u:= 0) AS u
ORDER BY user
) AS usertest
Table B) I'm getting select in following manner
Id user low_ID -------------------- 1 100 1 2 200 1 3 100 Null 4 300 1 5 300 Null 6 100 Null -------------------
I Want above code to permanently update low_id in sequence 1-n for every user record and then counter resets to 1 for next user and increment low id value by 1 for each record for that user .
I want to Update table A) like table c)
Table C)
Id user low_ID -------------------- 1 100 1 2 200 1 3 100 2 4 300 1 5 300 2 6 100 3 -------------------
simple.. order the data by user and count the number of times..
UPDATE users u,
( SELECT id, if(@a = user, @b:=@b+1, @b:=1) as new_low, @a:= user
FROM users
CROSS JOIN(SELECT @a:=0, @b:=1)t
ORDER BY user, id
) t1
SET u.low_id = t1.new_low
WHERE u.id = t1.id;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.