SQL连接中的过滤器重复 [英] filter duplicates in SQL join

查看:134
本文介绍了SQL连接中的过滤器重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用SQL连接时,是否可以只保留左表中只有一行的行?

When using a SQL join, is it possible to keep only rows that have a single row for the left table?

例如:

select * from A, B where A.id = B.a_id;

a1 b1
a2 b1
a2 b2

在这种情况下,我要删除除第一行以外的所有行,其中A中的单个行与B中的正好一行匹配。

In this case, I want to remove all except the first row, where a single row from A matched exactly 1 row from B.

我使用MySQL。 / p>

I'm using MySQL.

推荐答案

这应该在MySQL中使用:

This should work in MySQL:

select * from A, B where A.id = B.a_id GROUP BY A.id HAVING COUNT(*) = 1;

对于那些不使用MySQL的用户,需要使用聚合函数max())在所有的列(除了A.id),所以你的数据库引擎不抱怨。

For those of you not using MySQL, you will need to use aggregate functions (like min() or max()) on all the columns (except A.id) so your database engine doesn't complain.

这篇关于SQL连接中的过滤器重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆