我已经在我的数据库中添加了一些数据,我发现我有很多重复项,当然还有不同的密钥,我想将它们合并到一个记录中。
我想在sql数据库本身内进行,我不想截断表并再次插入值,没有重复,因为脚本很慢。
以下是我的情景示例:
表格跟踪:
key | artist | title
----|-----------|--------
k1 | artist1 | title1
----|-----------|--------
k2 | artist1 | title1
----|-----------|--------
k3 | artist1 | title1
表格图表:
trackKey | otherKey | anotherKey | value
---------|----------|--------------|---------
k1 | ok1 | ak4 | v1
---------|----------|--------------|---------
k3 | ok2 | ak2 | v2
---------|----------|--------------|---------
k1 | ok3 | ak9 | v2
---------|----------|--------------|---------
k2 | ok4 | ak1 | v6
其中chart.trackKey
引用track.key
我想要达到的结果是:
表格跟踪:
key | artist | title
----|-----------|--------
k1 | artist1 | title1
表格图表:
trackKey | otherKey | anotherKey | value
---------|----------|--------------|---------
k1 | ok1 | ak4 | v1
---------|----------|--------------|---------
k1 | ok2 | ak2 | v2
---------|----------|--------------|---------
k1 | ok3 | ak9 | v2
---------|----------|--------------|---------
k1 | ok4 | ak1 | v6
以便track
中相同条目的每个副本合并为一行,chart
中的旧密钥更新为track
表中唯一的一个。< / p>
有没有办法在SQL 中执行?
编辑:
解决方案#1基于@ popovits的回答
UPDATE chart c SET trackUri =
(WITH track_unique AS
(
SELECT MIN(uri) AS key, artist, title, album. artwork FROM track
GROUP BY artist, title
)
SELECT tu.key FROM chart c1
INNER JOIN track t ON c1.trackUri = t.key
INNER JOIN track_unique tu ON t.artist = tu.artist AND t.title = tu.title
WHERE c1.trackUri = c.trackUri and c1.countryId = c.countryId and c1.date = c.date);
返回
#1064 - Syntax error near
'track_unique AS (
SELECT MIN(uri) AS key, artist, title, album. artwork FR' line 2
解决方案#2基于@juergen d的回答
update chart
join track t1 on t1.uri = chart.trackUri
left join
(
select min(uri) as key
from track
group by artist, title
) tmp_track on tmp_track.key = chart.trackUri
set trackkey = tmp_tbl.key
where chart.trackUri not in
(
select min(uri)
from track
group by artist, title
having count(*) > 1
);
返回
#1064 - Syntax error near
'key
from track
group by artist, title
) tmp_track on tmp_track.key = c' line 5
我不知道自己做错了什么,所以我添加了架构定义(取自 phpMyAdmin )
答案 0 :(得分:1)
第一个with子句会获取您要保留的ID,然后在下一个选择查询中,您将这些ID与图表ID匹配。
我根据您对原始答案的修改编辑了这个答案。此答案假定chart(countryid,date)
唯一标识图表,并且仅当track(key,artist,title,album)
相等时才可合并这些曲目。
UPDATE chart c SET trackUri =
(WITH track_unique AS
(
SELECT MIN(uri) AS key, artist, title, album, artwork FROM track
GROUP BY artist, title, album, artwork
)
SELECT tu.key FROM chart c1
INNER JOIN track t ON c1.trackUri = t.key
INNER JOIN track_unique tu
ON t.artist = tu.artist
AND t.title = tu.title
AND t.album = tu.album
AND t.artwork = tu.artwork
WHERE c1.trackUri = c.trackUri
AND c1.countryId = c.countryId
AND c1.date = c.date);
在执行此更新后删除剩余的重复项:
DELETE FROM track
WHERE uri NOT IN
(SELECT MIN(uri) AS key, artist, title, album, artwork
FROM track
GROUP BY artist, title, album, artwork);
答案 1 :(得分:0)
如果重复值完全重复,则可以使用
SELECT MIN(key),artist,title FROM track GROUP BY artist,title;
在track
表中获取无重复版本的数据。您可以将它放在一个临时表中并将它们交换,或者使用您的SQL客户端下载数据并重新导入它,或者其他任何东西 - 为了安全起见,我不会尝试在一个语句中完成所有操作。