concating在MySQL
重命名重复行我有一个表像下concating在MySQL
ID student_name dept email 1 Mary Wise Eng [email protected]
2 John Walter Sc [email protected]
3 Sophia Jacob Politics [email protected]
4 Ava William Eng [email protected]
5 Mary Wise Politics [email protected]
6 John Walter Eng [email protected]
7 John Walter Politics [email protected]
8 Sophia Eng [email protected]
9 Emma Eng [email protected]
10 Sherlock Eng [email protected]
生成的电子邮件ID山坳通过[email protected] 问题是当名称是相同的电子邮件ID也是相同。 我想要电子邮件ID附加1,2,3时存在相同的名称。
For example in table above the mary-wise on 5th row should be [email protected],
6th row should be, [email protected],
7th row should be, [email protected]
如何尽可能快地使用mysql查询更新我的电子邮件列。 我尝试使用php与MySQL它需要太长,当表中包含百万行。
感谢
回答:
下面的SQL将枚举重复:使用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;
你可以把这个在update
:
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);
回答:
我认为这是更好地为您做出email
列是唯一的,并使用ON DUPLICATE KEY UPDATE
语法(更多here)。
您仍然需要跟踪要附加到新值的数字。为此,您可以创建一个带有自动增量字段的独立表格,并从中获取新值。
回答:
,如果你有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
以上是 concating在MySQL 的全部内容, 来源链接: utcz.com/qa/264485.html