mysql - SQL:在 Assets 列表中查找不同的帐户

标签 mysql sql select

我的目标是一个 SQL 语句来识别 Assets 列表中的不同帐户( Assets 有一个主编号和一个子编号)。

表格如下所示:

amainnr | asubnr | account
-----------------------------
10000   | 0      | 123
10000   | 1      | 123
10000   | 2      | 456
10000   | 3      | 789
10001   | 0      | 123
10001   | 1      | 123
10001   | 2      | 123
10002   | 0      | 123
10003   | 0      | 456
10004   | 0      | 123
10005   | 0      | 123
10005   | 1      | 456

因此,我需要一个包含不同帐户的 mainnr 的所有行的表格,例如:

amainnr | asubnr | account
-----------------------------
10000   | 0      | 123
10000   | 1      | 123
10000   | 2      | 456
10000   | 3      | 789
10005   | 0      | 123
10005   | 1      | 456

我用这个表创建了一个 SQL Fiddle:http://sqlfiddle.com/#!2/c7e6d

我尝试了很多 GROUP BY、HAVING 和 COUNT,但到目前为止我没有成功。 这个问题完全可以用 SQL 解决吗?

如有任何帮助,我们将不胜感激!

最佳答案

SELECT b.amainnr,
       b.asubnr,
       b.account
FROM atable b
WHERE b.amainnr IN
    (SELECT a.amainnr
     FROM atable a
     GROUP BY a.amainnr HAVING count(distinct(a.account)) > 1)

关于mysql - SQL:在 Assets 列表中查找不同的帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25759490/

相关文章:

mysql - 在 mysql 服务器上触发两个查询会出现错误

php - Adodb:Postgresql 从两个表中选择但返回一组结果

php - 你怎么写“Select (all) From (table) Where posting=$posting except this posting?(Mysql)

mysql - 来自 varchar 的最大值

java - Glassfish 无法连接到远程 MySQL,但 localhost 可以

MySQL where 子句后的序号

MySQL服务器存储过程语法错误

database - Oracle SELECT - 双引号还是没有双引号?

mysql - SQL与模糊比较

ios - 如何为 iOS 编译 Qt 的 MySQL 驱动程序