mysql - 用于查找共享某些值但不共享其他值的对的 SQL 查询

标签 mysql sql distinct

我在为这个问题创建查询时遇到了一些问题。

我需要找到哪些人有WorkedOn Building but have not Financed together.

我的 table :

Build(person, financed, workedOn)

Person | Financed | WorkedOn
-----------------------------
Person1| Build1   | Build4
Person2| Build2   | Build5
Person3| Build1   | Build5
Person4| Build2   | Build3
Person5| Build3   | Build1
Person6| Build1   | Build5
Person7| Build4   | Build3
Person8| Build5   | Build3

正确的查询应该产生这样的结果:

Person
-------
Person7  Person8
Person8  Person4
Person6  Person3

我试过使用 distincthaving 我只是觉得不太对劲。任何帮助将不胜感激,我正在使用 sqlite3,谢谢 :)

最佳答案

SELECT t1.person, t2.person
FROM build t1, build t2
WHERE t1.workedon = t2.workedon
AND t1.financed != t2.financed
AND t1.person > t2.person

SQL Fiddle

关于mysql - 用于查找共享某些值但不共享其他值的对的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33711064/

相关文章:

c# - 在 C# 中使用 LINQ 获取 List<T> 中不同属性值的数量

PHP:mysqli_stmt_num_rows($stmt) 始终返回 0

MySQL查询合并重复键(2个表)

PHPExcel 获取数据库字段名称到导出的 Excel

mysql - 选择连接条件错误的查询

xml - 使用 xslt 删除 xml 中的重复项

php - 如何使用 MYSQL "LIKE"正确检查关键字

sql - SQL Server 中的递归查询问题

mysql - 为其他两列的每个唯一组合选择具有 Max(列值)的行

MySQL DISTINCT 返回不那么不同的结果