我有一个查询成功分析了我的表,找到了与所讨论的字段100%匹配的重复项,并返回重复项的计数。现在我需要编写一个返回记录ID值的查询,以便我可以删除它们。这是我的初步查询:
SELECT CompanyID, COUNT(*) AS dupecount
FROM artist_reports
GROUP BY contemporary, traditional, country, folk, functional, decorative, abstract, representational, figurative, price_range_low,
price_range_high, best_selling_range_low, best_selling_range_high, average_sales_other_shows, total_sales_this_event, average_exhibitor_quality, financial_fairness, patrons_art_savvy, demographics, buying_energy,
advertising, venue_environment, show_layout, organization, director_support, staff_support, logistical_ease, load_in_out, parking_ease, artist_amenities,
awards_judging, security_efficiency, weather, event_year, critique, artist_reports.status, public_email, artist_reports.email, would_you_return, fairs_per_year, CompanyID
HAVING COUNT(*) > 1
理论上,我有一个链接到另一个传递CompanyID的页面,然后删除重复项。但是,我尝试了这个,它只返回一条记录:
SELECT arid FROM artist_reports WHERE arid IN (
SELECT * FROM (
SELECT arid
FROM artist_reports
WHERE CompanyID = 12345
GROUP BY contemporary, traditional, country, folk, functional, decorative, abstract, representational, figurative, price_range_low,
price_range_high, best_selling_range_low, best_selling_range_high, average_sales_other_shows, total_sales_this_event, average_exhibitor_quality, financial_fairness, patrons_art_savvy, demographics, buying_energy,
advertising, venue_environment, show_layout, organization, director_support, staff_support, logistical_ease, load_in_out, parking_ease, artist_amenities,
awards_judging, security_efficiency, weather, event_year, critique, artist_reports.status, public_email, artist_reports.email, would_you_return, fairs_per_year, CompanyID
HAVING COUNT(*) > 1) AS a )
我不确定我做错了什么,但理想情况下我想为报告( arid )获取记录集的唯一ID值,然后迭代这些值来删除最新的,原始完整。
答案 0 :(得分:1)
在oracle中你可以使用窗口函数,但在这里你需要作弊。
我认为您可能希望删除arid
的重复项。要使用您的查询获取所有ID,请尝试向其添加GROUP_CONCAT
:
SELECT CompanyID, COUNT(*) AS dupecount,
GROUP_CONCAT(arid) AS all_duplicates_ids
FROM artist_reports
GROUP BY contemporary, traditional, country, folk, functional, decorative, abstract, representational, figurative, price_range_low,
price_range_high, best_selling_range_low, best_selling_range_high, average_sales_other_shows, total_sales_this_event, average_exhibitor_quality, financial_fairness, patrons_art_savvy, demographics, buying_energy,
advertising, venue_environment, show_layout, organization, director_support, staff_support, logistical_ease, load_in_out, parking_ease, artist_amenities,
awards_judging, security_efficiency, weather, event_year, critique, artist_reports.status, public_email, artist_reports.email, would_you_return, fairs_per_year, CompanyID
HAVING COUNT(*) > 1
现在你将在结果中有类似的东西(假设有5个重复):
CompanyID|dupecount|all_duplicates_ids
---------------------------------------
12345 | 5 | '2,5,8,9, 12'
您不想删除所有5条记录,只需删除其中的4条,因此您可以修改此字符串,方法是将GROUP_CONCAT
部分替换为:
SUBSTRING_INDEX(
CONCAT(
GROUP_CONCAT(arid),
','),
',', 1) AS all_duplicates_without_one
现在你有了带有ids的昏迷分隔字符串。您可以在where
子句中的删除查询中使用它 - 我建议使用FIND_IN_SET
函数检查arid
是否在逗号分隔的字符串中:
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set
答案 1 :(得分:0)
考虑此查询以获取您要保留的行:(我假设您要保留最少的行arid)
SELECT min(arid) as Original_ID
,
contemporary, traditional, country, folk, functional, decorative, abstract, representational, figurative, price_range_low,
price_range_high, best_selling_range_low, best_selling_range_high, average_sales_other_shows, total_sales_this_event, average_exhibitor_quality, financial_fairness, patrons_art_savvy, demographics, buying_energy,
advertising, venue_environment, show_layout, organization, director_support, staff_support, logistical_ease, load_in_out, parking_ease, artist_amenities,
awards_judging, security_efficiency, weather, event_year, critique, artist_reports.status, public_email, artist_reports.email, would_you_return, fairs_per_year, CompanyID
FROM artist_reports
where CompanyID=12345
GROUP BY contemporary, traditional, country, folk, functional, decorative, abstract, representational, figurative, price_range_low,
price_range_high, best_selling_range_low, best_selling_range_high, average_sales_other_shows, total_sales_this_event, average_exhibitor_quality, financial_fairness, patrons_art_savvy, demographics, buying_energy,
advertising, venue_environment, show_layout, organization, director_support, staff_support, logistical_ease, load_in_out, parking_ease, artist_amenities,
awards_judging, security_efficiency, weather, event_year, critique, artist_reports.status, public_email, artist_reports.email, would_you_return, fairs_per_year, CompanyID
HAVING COUNT(*) > 1
然后,将该查询与原始表连接以获得所需的行:
select a_r.arid from
artist_reports as a_r
inner join
(
SELECT min(arid) as Original_ID
,
contemporary, traditional, country, folk, functional, decorative, abstract, representational, figurative, price_range_low,
price_range_high, best_selling_range_low, best_selling_range_high, average_sales_other_shows, total_sales_this_event, average_exhibitor_quality, financial_fairness, patrons_art_savvy, demographics, buying_energy,
advertising, venue_environment, show_layout, organization, director_support, staff_support, logistical_ease, load_in_out, parking_ease, artist_amenities,
awards_judging, security_efficiency, weather, event_year, critique, artist_reports.status, public_email, artist_reports.email, would_you_return, fairs_per_year, CompanyID
FROM artist_reports
where CompanyID=12345
GROUP BY contemporary, traditional, country, folk, functional, decorative, abstract, representational, figurative, price_range_low,
price_range_high, best_selling_range_low, best_selling_range_high, average_sales_other_shows, total_sales_this_event, average_exhibitor_quality, financial_fairness, patrons_art_savvy, demographics, buying_energy,
advertising, venue_environment, show_layout, organization, director_support, staff_support, logistical_ease, load_in_out, parking_ease, artist_amenities,
awards_judging, security_efficiency, weather, event_year, critique, artist_reports.status, public_email, artist_reports.email, would_you_return, fairs_per_year, CompanyID
HAVING COUNT(*) > 1
) as duplicates
on
a_r.contemporary=duplicates.contemporary
and
a_r.traditional=duplicates.traditionals
and
.
.
.
where
a_r.arid <> traditional.Original_ID
当然你应该直接在上面的查询中使用delete语句:
delete from article_reports as a_r
inner join .......