在给定的日期范围内使用相同的组合计数

时间:2014-04-05 09:26:43

标签: mysql sql

您好我有2个表进攻表和User_jobs表

进攻表:

crime_id |crime_type |casenumber| date       |
---------+-----------+----------+------------+
1        | 3         |1         |2011        |
2        | 3         |1         |2010        |
1        | 3         |2         |2012        |
12       | AA        |2         |2011        |

user_jobs表:

casenumber |disposal_status |
-----------+----------------+
1          | yes            |
1          | yes            |
2          | no             |
2          | no             |

我想要的是计算具有相同组合但在给定日期范围内的行数,比如说2010_2012中的crime_id = 1和crime_type = 3,但这些必须在user_jobs表中具有处置状态为

1 个答案:

答案 0 :(得分:1)

SELECT crime_id,crime_type,count(*) as numberOfRows
FROM offense_table o
INNER JOIN users_table u
      ON o.casenumber=u.casenumber
WHERE u.disposal_status='yes' 
      AND `date` BETWEEN 2010 AND 2012
GROUP BY crime_id,crime_type

OUTPIT

crime_id |crime_type |numberOfRows
---------+-----------+----------+
1        | 3         |1         |
2        | 3         |1         |