MySQL的distinct查询返回有重复信息的行,需要去重
我在 MySQL 数据库中有一个类似于下面所示的表:
+----------+----------+----------+----------+----------+ | Column_A | Column_B | Column_C | Column_D | Column_E | +----------+----------+----------+----------+----------+ | 1 | 11 | a | 0 | abc | | 2 | 22 | a | 0 | abc | | 3 | 33 | a | 0 | def | | 4 | 44 | b | 0 | def | | 5 | | b | 0 | def | | 6 | 55 | c | 0 | ghi | | 7 | | d | 0 | jkl | | 8 | | a | 4 | abc | | 9 | | a | 4 | abc | | 10 | | b | 4 | abc | | 11 | 88 | f | 4 | xyz | | 12 | | f | 4 | xyz | +----------+----------+----------+----------+----------+
我需要一个类似于下面的结果(即只有 a 和 b 值具有不同的列 D 和 E 值):
+----------+----------+----------+ | Column_C | Column_D | Column_E | +----------+----------+----------+ | a | 0 | abc | | a | 0 | def | | a | 4 | abc | | b | 0 | def | | b | 4 | abc | +----------+----------+----------+
我尝试过这个查询:
SELECT DISTINCT column_c,column_d,column_e FROM trial2 ORDER BY column_c;
我得到这个:
+------------------+------------------+------------------+ | column_c | column_d | column_e | +------------------+------------------+------------------+ | a | 0 | abc | | a | 0 | def | | a | 4 | abc | | b | 0 | def | | b | 4 | abc | | c | 0 | ghi | | d | 0 | jkl | | f | 4 | xyz | +------------------+------------------+------------------+
我不需要column_c中带有“c”、“d”或“f”的行。我需要同时具有 0 和 0 的行column_d 中有 4 个值(即,column_c 是“a”或“b”)。
I have a table similar to the one shown below in a MySQL database:
+----------+----------+----------+----------+----------+ | Column_A | Column_B | Column_C | Column_D | Column_E | +----------+----------+----------+----------+----------+ | 1 | 11 | a | 0 | abc | | 2 | 22 | a | 0 | abc | | 3 | 33 | a | 0 | def | | 4 | 44 | b | 0 | def | | 5 | | b | 0 | def | | 6 | 55 | c | 0 | ghi | | 7 | | d | 0 | jkl | | 8 | | a | 4 | abc | | 9 | | a | 4 | abc | | 10 | | b | 4 | abc | | 11 | 88 | f | 4 | xyz | | 12 | | f | 4 | xyz | +----------+----------+----------+----------+----------+
I need a result similar to the one below (i.e only a & b values have different column D & E values):
+----------+----------+----------+ | Column_C | Column_D | Column_E | +----------+----------+----------+ | a | 0 | abc | | a | 0 | def | | a | 4 | abc | | b | 0 | def | | b | 4 | abc | +----------+----------+----------+
I have tried this query:
SELECT DISTINCT column_c,column_d,column_e FROM trial2 ORDER BY column_c;
I get this:
+------------------+------------------+------------------+ | column_c | column_d | column_e | +------------------+------------------+------------------+ | a | 0 | abc | | a | 0 | def | | a | 4 | abc | | b | 0 | def | | b | 4 | abc | | c | 0 | ghi | | d | 0 | jkl | | f | 4 | xyz | +------------------+------------------+------------------+
I do not need the rows with 'c', 'd' or 'f' in column_c. I need rows which have both 0 & 4 values in column_d (i.e. column_c is 'a' or 'b').
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不需要加入...
having 子句在应用聚合后运行,因此您可以过滤分组后剩余的行数...
You don't need to join...
The having clause operates after the aggregate is applied, so you can filter on how many rows are left after grouping...
不太确定这个解决方案,但我认为它符合你的要求。
询问:
Not too sure about this solution, but I think it does what you want.
Query:
DISTINCT
仅确保一行在输出中最多出现一次。它不会删除与其他行不完全匹配的行。要同时操作多行,您需要一个内部联接:
内部联接会过滤掉没有匹配行的行。在上面的查询中,匹配行是那些 C 列具有相同值但 D 或 E 列不同的行。
DISTINCT
merely ensures a row appears at most once in the output. It won't remove rows that don't exactly match other rows.To operate on more than one row at once, you'll need an inner join:
An inner join filters out rows that don't have a matching row. In the above query, matching rows are those that have the same value for column C, but differ in column D or E.