我的表 person_log
包含以下字段:
id
时间戳
名字
姓氏
操作
一些示例数据
1;012545878;homer;simpson;eating
2;812345222;homer;simpson;watching tv
3;312322578;marge;simpson;cooking
4;114568568;bart;simpson;skating
5;015345345;bart;simpson;skating
现在我需要选择 first_name
和 last_name
相同且至少出现两次但 action
不同的所有记录集。
导致选择 id
1 和 2。
我怎样才能做到这一点? TIA?
最佳答案
使用派生表获取至少有 2 个不同操作的人员,并将其连接到原始表以获取结果中的其他列。
select p.*
from person_log p
join (select first_name,last_name
from person_log
group by first_name,last_name
having count(*) >=2 and count(distinct action) >= 2) p1
on p1.first_name=p.first_name and p1.last_name=p.last_name
关于mysql - 如何通过 SQL 选择一个字段重复且另一字段不同的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39985550/