[英]How can I count the number of times an item occurs in a given date in mysql
因此,我有一个称为违规者的表,并且有2列:违规和日期。 违规者:
---------------------------
Violation | Date
---------------------------
overspeeding | 2016-05-05
overloading | 2016-05-07
overspeeding | 2016-05-05
drunk driving | 2016-05-03
我想知道在2016-05-05中发生了超速几次,所以答案应该是:2016-05-05-超速-2
如果我想知道,超载在2016-05-07中发生了多少次,答案应该是:2016-05-07-超载-1
您的表格中会针对每个overspeeding
记录添加一个条目。
因此执行一个SQL查询(例如用PHP)
$resultset = select * from tablename where Violation ='overspeeding' And Date ='2016-05-05'
这将给您两个记录,因此只需在结果集上应用一个count函数即可。在mysql中,使用php,它看起来像这样
$temp_variable_no_of_overspeeding =mysql_num_rows($resultset);
// or you can use count function if don't need those values
所以你的临时变量中会有2
我猜你正在使用Java,所以这样做
ResultSet rs = ps.executeQuery();
int rowcount = 0;
if (rs.last()) {
rowcount = rs.getRow();
rs.beforeFirst(); // required to read data in while later . not rs.first()
//because the rs.next() below will move on 2nd element ,
//you will miss the first element
}
while (rs.next()) {
// do your standard per row stuff
}
我已经解决了这个问题:这是Sql查询:
从“违规者”,“违规日期”,“违规者”组中选择“违规,计数(违规),Date_Violated”
谢谢你们的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.