我的people (table)
中有超过1100条记录,我希望过滤特定place (table)
记录下的记录,并更新名为people_identifier_num
people
的新插入列值{1}}表。
放置表格
id name desc
1 Place 1 some description.
2 Place 2 some description..
3 Place 3 some description...
人员表
id name place_id people_identifier_num
1 People 1 1 null
2 People 2 1 null
3 People 3 2 null
4 People 4 2 null
5 People 5 2 null
6 People 6 2 null
7 People 7 3 null
... ... ... ....
1100 People 1100 2 null
现在我想将 pin(people_identifier_num)分配给属于以值1开头的特定地点的每个人,依此类推。
E.g。
id name place_id people_identifier_num
3 People 3 2 1
4 People 4 2 2
5 People 5 2 3
6 People 6 2 4
... ... ... ....
E.g。
id name place_id people_identifier_num
1 People 1 1 1
2 People 2 1 2
... ... ... ....
我目前的解决方案是按地点过滤人员并按字母顺序对其进行排序。但是,我们如何编写一个循环每个已过滤记录的SQL脚本,并从1开始分配引脚值,依此类推?
答案 0 :(得分:1)
您可以使用排名查询来分配引号。根据他们的位置给每个人
update people
join (
select p.id,place_id,name,
case when @place = place_id then @row:= @row +1 else @row:= 1 end rank,
@place:= place_id
from (
select * from people,(select @place:=null,@row:=1) t order by place_id,name
) p
) p1
using(place_id,name)
set people_identifier_num = p1.rank;