我有一张包含以下记录的表
id | fname | lname | skills
22 | Jane | Doe | php,mysql
43 | Jane | Doe | java,oracle,php
45 | Jane | Doe | mongo,mysql
id是主键。我想执行一个查询,以便在表中看起来像这样
id | fname | lname | skills
45 | Jane | Doe | php,mysql,java,oracle,mongo
技能将与最新的id和其他重复记录合并
任何帮助对我都非常有用。
答案 0 :(得分:2)
试试这个:
UPDATE YourTable t
INNER JOIN(SELECT max(s.id) as max_id,s.fname,s.lname,GROUP_CONCAT(distinct(s.skills)) as skill_str
FROM YourTable s
GROUP BY s.fname,s.lname) t1
ON (t.lname = t1.lname and t.fname = t1.fname AND t.id = t1.max_id)
SET t.skills = t1.skill_str
这会将表更新为所需的concat,然后删除:
DELETE FROM YourTable t
WHERE t.ID NOT IN(SELECT MAX(s.id) FROM YourTable s
GROUP BY s.lname,s.fname)
只有当ID
是唯一的时,删除才有效!它将删除所有不是最大ID
编辑:试试这个:
DELETE t
FROM candidate t
LEFT JOIN(SELECT MAX(s.candidate_id) as max_id FROM candidate s
GROUP BY s.fname,s.lname) t1
ON (t.candidate_id = t1.max_id)
WHERE t1.max_id is null
答案 1 :(得分:1)
试试这个,
SELECT
MAX(id) AS id, fname, lname,
GROUP_CONCAT(DISTINCT(skills)) AS skills
FROM
< your_table >
GROUP BY
fname, lname
参考文献:
http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
答案 2 :(得分:0)
假设您每行有10个技能限制,您可以使用以下查询规范化skills
列:
select distinct
s.fname, s.lname,
replace(
substring(substring_index(s.skills, ',', p.pos),
char_length(substring_index(s.skills, ',', p.pos -1)) + 1),
',', ''
) as skill
from
skills s
cross join
(select 1 pos
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 10
) p on p.pos <= (char_length(s.skills) - char_length(replace(s.skills, ',', ''))) + 1
结果:
| fname | lname | skill |
|-------|-------|--------|
| Jane | Doe | php |
| Jane | Doe | java |
| Jane | Doe | mongo |
| Jane | Doe | mysql |
| Jane | Doe | oracle |
(char_length(s.skills) - char_length(replace(s.skills, ',', ''))) + 1
将返回技能数量(计算逗号)。
replace(
substring(substring_index(s.skills, ',', p.pos),
char_length(substring_index(s.skills, ',', p.pos -1)) + 1),
',', ''
)
将在特定位置提取技能。
您可以使用具有不间断数字序列的任何现有表格,而不是创建内联表格。
要再次对结果进行非规范化,您可以使用GROUP_CONCAT(DISTINCT skill)
:
select
s.fname, s.lname,
group_concat(distinct replace(
substring(substring_index(s.skills, ',', p.pos),
char_length(substring_index(s.skills, ',', p.pos -1)) + 1),
',', ''
)) as skills
from skills s
cross join (
select 1 pos
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 10
) p on p.pos <= (char_length(s.skills) - char_length(replace(s.skills, ',', ''))) + 1
group by s.fname, s.lname
结果:
| fname | lname | skills |
|-------|-------|-----------------------------|
| Jane | Doe | php,mysql,mongo,oracle,java |
这样,列表中没有两次技能出现。