1
A
回答
4
有,你可以得到结果的几种方法。
使用HAVING子句与聚合函数:
select personid, race
from yourtable
where personid in
(select personid
from yourtable
group by personid
having
sum(case when race = 'white' then 1 else 0 end) > 0
and sum(case when race = 'asian' then 1 else 0 end) > 0
and sum(case when race not in('asian', 'white') then 1 else 0 end) = 0);
或者您也可以在HAVING子句中使用count(distinct race)
:
;with cte as
(
select personid
from yourtable
where race in ('white', 'asian')
and personid not in (select personid
from yourtable
where race not in ('white', 'asian'))
group by personid
having count(distinct race) = 2
)
select personid, race
from yourtable
where personid in (select personid
from cte);
2
0
非常天真,但应该工作。
select * from yourtable t1 join yourtable t2 on t1.id = t2.id where ((t1.race = 'Asian' and t2.race = 'White') OR (t1.race = 'White' and t2.race = 'Asian')) and t1.id not in (select id from yourtable where race not in ('Asian','white'));
相关问题
- 1. 不包括匹配条件的选择查询中的记录
- 2. PostgreSQL的:选择记录,其中的所有相关记录匹配条件
- 3. 根据一条记录的列值选择多条记录
- 4. 为条件选择一条记录
- 5. MySQL仅选择一条记录加入,包括不匹配
- 6. 选择与模式匹配的记录
- 7. 如何在单个条目匹配时选择整个记录
- 8. 只选择一条记录
- 9. 条件选择记录 - Joomla
- 10. 选择一个行匹配条件或第一个匹配条件,如果没有任何匹配条件
- 11. 在子报表中将记录选择匹配到父记录
- 12. SQL选择一列中匹配多个条件的所有行
- 13. 在同一张表中选择不匹配的记录 - SQL
- 14. 的MySQL选择记录如果其他记录不匹配
- 15. 匹配只是一条记录
- 16. 选择阶段从表匹配表一个匹配一号两个记录
- 17. MySQL的选择第一条记录
- 18. 选择查询返回一个匹配的记录
- 19. MySQL选择与数值匹配的唯一记录
- 20. 查询要从多条记录中选择一条记录
- 21. 根据组中的一条记录选择一组记录
- 22. SQL选择匹配的行成两列,每一列都匹配
- 23. Rails活动记录 - 找到记录,其中两列的总和匹配条件
- 24. 在yii2中的最后一条记录之前选择一条
- 25. SQL - 多表连接并选择条件的第一条记录
- 26. 选择前10条记录,然后选择下一条10
- 27. Awk - 匹配记录后打印下一条记录
- 28. 按最少记录数(COUNT)匹配条件筛选
- 29. 在一个表格中选择不在另一个模式匹配的记录
- 30. SQL - 选择第一个匹配记录加入
谢谢!我喜欢第二种解决方案,因为这看起来很容易通过一些输入参数来插入比赛值和计数值。 – gooseman