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