查找具有多个不同的列值的记录

时间:2018-07-28 06:20:43

标签: sql sqlite

给表func CallAPI() { let url = URL(string: "https:// your_Address")! let request = URLRequest(url: url) let task = URLSession.shared.dataTask(with: request) { data, response, error in if let _ = response, let data = data { let result = String(data: data, encoding: .utf8)! print(result as NSString) } else { print(error as Any) } } task.resume() } 加上列t1(文本,主键)和Id(文本),如下所示。

place

我需要找出具有多个不同+-------+-----------+ | Id | place | +-------+-----------+ | abcde | Santori | | bcdef | Krypt | | cdefg | Bali | | defgh | Bangkok | | abcde | Colombo | +-------+-----------+ 的{​​{1}}的记录。在上面的示例中,输出应为

Id

3 个答案:

答案 0 :(得分:2)

我会使用exists

select t.*
from table t
where exists (select 1 from table t1 where t1.id = t.id and t1.place <> t.place);

答案 1 :(得分:1)

我认为对您来说还行:

SELECT ID, PLACE FROM T1 as A 
WHERE A.ID IN 
(SELECT ID FROM T1 AS B 
GROUP BY ID 
HAVING count(*) > 1
)

答案 2 :(得分:1)

在子查询中,您需要获取count中的distinct place并获取ID。然后使用外部查询来获取所有记录。

Fiddle Example

select * From T1
where T1.ID in
(select ID from T1
 group by ID
having count(distinct PLACE) > 1
)