[英]count number of rows that have the same combination of two colums
嗨,我有2表進攻表和User_jobs表
offense table:
crime_id |crime_type |casenumber|
---------+-----------+----------+
1 | 3 |1 |
2 | 3 |1 |
1 | 3 |2 |
12 | AA |2 |
user_jobs table:
casenumber |disposal_status |
-----------+----------------+
1 | yes |
1 | yes |
2 | no |
2 | no |
我想要的是計算具有相同組合的行數,例如Crime_id = 1和Crime_type = 3,但是這些行在user_jobs表中的處置狀態必須為yes。
我想在mysql中做到這一點。 PLIZ幫助
抱歉,但是我是mysql的新手。 我現在想顯示這些ID的真實姓名,而不是ID本身。
具有這些ID的表是Crime_category和Crime_type Crime_catgory
table:
category |crime_id |
-----------+----------------+
theft | 1 |
murder | 2 |
rape | 3 | 2 |
no |
Crime_type table:
Crime_type |id |
---------------+----------------+
administrative | yes |
criminal | yes |
您可以通過一個簡單的內部聯接和一個聚合函數來做到這一點:
select
o.crime_id,
o.crime_type,
count(*)
from
offence o
join user_jobs uj
on o.casenumber=uj.casenumber
where
uj.disposal_status='Yes'
group by
o.crime_id,
o.crime_type
這將拾取前兩列的獨特組合,因為它們應該合並到工作表中,並且僅在displacement_status等於“ Yes”的情況下
編輯:對於這種情況,我可能會做得非常好,因為我閱讀了我整理的此問題與解答-在此處為您提供了代碼,但想在此解釋更多細節。 問答解釋了這種事情(以及許多其他事情)為什么起作用以及它們如何起作用:
編輯2:
select
o.crime_id,
o.crime_type,
ct.category,
count(*)
from
offence o
join user_jobs uj
on o.casenumber=uj.casenumber
join crime_type ct
on o.crime_type=ct.crime_id
where
uj.disposal_status='Yes'
group by
o.crime_id,
o.crime_type,
ct.category,
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.