在 MySQL SELECT 语句中计算重复项并删除它们 [英] Counting duplicates and removing them in MySQL SELECT Statement
问题描述
我在处理这个问题时遇到了一些麻烦.
I am having some trouble wrapping my head around this one.
I have a Cars Database with a Class field that when selecting all entries, it will have duplicates of each Class value.我需要做的是计算每个 Class 值的所有重复项,然后删除重复项以仅显示每个 Class 值一次,其中包含计数的自定义字段量.
I have a Cars Database with a Class field that when selecting all entries, it will have duplicates of each Class value. What I need to do with that is to count all of duplicates for each Class value and then remove the duplicates to only display each Class value once with a custom fields with the counted amount.
Class
-----
A3
A3
A3
A3
B2
B2
成为:
Class | Count
------+---------
A3 | 4
B2 | 2
我可以想象某些东西包含 Count()
方法,但我不知道如何让它只计算重复值并为每个 Class 值分别返回值?
I could imagine something contain the Count()
method, but I have no idea how I could have it count only the duplicate values and return the values separately for each Class value?
在此先感谢您对此的任何帮助.
Thanks in advance for any help in this one.
推荐答案
SELECT Class,COUNT(1) "Count"
FROM Cars GROUP BY Class
HAVING COUNT(1) > 1;
这篇关于在 MySQL SELECT 语句中计算重复项并删除它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!