mysql - 怎样才能拥有一切的人呢? mysql

标签 mysql

下表为

person  things
A   TV
A   CAR
A   MOBILE
A   HOUSE
B   TV
C   TV
C   CAR
C   MOBILE
C   HOUSE
D   TV
E   TV
F   TV

在上面我可以看到 A 和 C 拥有所有的东西。

根据书本示例,我找到了 每个人都有什么东西?使用下面的查询,

SELECT things
FROM tbl_happiness as x 
WHERE NOT EXISTS 
( 
SELECT person 
FROM tbl_happiness as y 
WHERE NOT EXISTS 
( 
SELECT 1
FROM tbl_happiness as z 
WHERE z.person = y.person 
AND z.things = x.things
 )
);

是否可以使用更改别名的相同查询来查找

哪个人拥有所有的东西?

最佳答案

我使用group byhaving来处理这些“set-within-sets”查询:

select person
from table t
group by person
having count(distinct thing) = (select count(distinct thing) from table t);

如果没有重复项,您可以使用:

having count(*) = (select count(distinct thing) from table t);

关于mysql - 怎样才能拥有一切的人呢? mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29319245/

相关文章:

mysql - 如何将form_dropdown id值添加到数据库? - 代码点火器

mysql - 错误: Cannot delete or update a parent row: a foreign key constraint fails

python - 一个奇怪的空间问题 Python

mysql - 如何从 MySQL .NET Connector 使用 SSL 访问托管在 Amazon RDS 上的 MySQL 数据库

mysql - 访问网站的平均日期

PHP 断开和 mysql 连接

php - 表单提交未将数据输入数据库

mysql - Powershell 中 MySQL 的奇怪行为

MySQL根据两行数据选择计数

sql - 什么时候应该为 SQL 表提供自己的数字主键?