在mysql中重命名重命名的行

时间:2014-01-15 12:09:58

标签: php mysql

我有一个如下表

ID   student_name        dept        email

1    Mary Wise           Eng         mary-wise@xxx.cc
2    John Walter         Sc          john-walter@xxx.cc
3    Sophia Jacob        Politics    sophia-jacob@xxx.cc
4    Ava William         Eng         ava-william@xxx.cc
5    Mary Wise           Politics    mary-wise@xxx.cc
6    John Walter         Eng         john-walter@xxx.cc
7    John Walter         Politics    john-walter@xxx.cc
8    Sophia              Eng         sophia@xxx.cc
9    Emma                Eng         emma@xxx.cc
10   Sherlock            Eng         sherlock@xxx.cc

电子邮件ID col由firstname-lastname@xxx.cc生成 问题是当名称相同时,电子邮件ID也相同。 我希望当存在相同名称时,电子邮件ID将附加1,2,3。

For example in table above 
the mary-wise on 5th row should be mary-wise1@xxx.cc, 
6th row should be, john-walter1@xxx.cc, 
7th row should be, john-walter2@xxx.cc

如何尽快使用mysql查询更新我的电子邮件列。 我尝试使用带有mysql的php,当表包含数百万行时,它需要太长时间。

由于

3 个答案:

答案 0 :(得分:0)

以下SQL将枚举重复项:

select t.*,
       @rn := if(@StudentName = StudentName, 1, @rn + 1) as seqnum,
       @StudentName := StudentName
from table t cross join
     (select @rn := 0, @StudentName := '') const
order by StudentName;

您可以使用update

将其放在join
update t join
       (select t.*,
           @rn := if(@StudentName = StudentName, 1, @rn + 1) as seqnum,
           @StudentName := StudentName
        from table t cross join
             (select @rn := 0, @StudentName := '') const
        order by StudentName
       ) toupdate
       on t.name = toupdate.name and toupdate.seqnum > 1
    set email = concat(replace(t.StudentName, ' ', '-'), toupdate.seqnum - 1, '@xxx.cc);

答案 1 :(得分:0)

我认为最好让email列唯一并使用ON DUPLICATE KEY UPDATE语法(更多here)。

您仍需要跟踪要附加到新值的数字。为此,您可以使用自动增量字段创建一个单独的表,并从那里获取新值。

答案 2 :(得分:0)

如果您有CTE(如果可以,可以切换到postgres 9),这将很容易实现:

SELECT
 id
 , student_name
 , concat(
   replace(lower(student_name), ' ', '-')
   , case 
     when cnt > 1 then numb
   end
   ,'@xxx.cc'
 ) as newmail
FROM (
 SELECT
 count(*) over (partition BY student_name) as cnt
 , count(*) over (partition BY student_name order by id)  as numb 
 , id
 , student_name
 FROM tab1
 order by id
 ) subq

sqlFiddle demo