[英]How can I count the number of times an item occurs in a given date in mysql
So I have a table called violators, and I have 2 columns: Violation and Date. 因此,我有一个称为违规者的表,并且有2列:违规和日期。 Violators:
违规者:
---------------------------
Violation | Date
---------------------------
overspeeding | 2016-05-05
overloading | 2016-05-07
overspeeding | 2016-05-05
drunk driving | 2016-05-03
I want to know how many times the overspeeding occured in 2016-05-05, So the answer should be: 2016-05-05 - overspeeding - 2 我想知道在2016-05-05中发生了超速几次,所以答案应该是:2016-05-05-超速-2
And if I want to know, how many times the oversloading occured in 2016-05-07, The answer should be: 2016-05-07 - overloading - 1 如果我想知道,超载在2016-05-07中发生了多少次,答案应该是:2016-05-07-超载-1
there will be an entry in your table against every overspeeding
record. 您的表格中会针对每个
overspeeding
记录添加一个条目。
so execute a sql query (eg with php) 因此执行一个SQL查询(例如用PHP)
$resultset = select * from tablename where Violation ='overspeeding' And Date ='2016-05-05'
this will give you two record so simply apply a count function on result set.in mysql with php ,it will look like this 这将给您两个记录,因此只需在结果集上应用一个count函数即可。在mysql中,使用php,它看起来像这样
$temp_variable_no_of_overspeeding =mysql_num_rows($resultset);
// or you can use count function if don't need those values
so will have 2 in your temp variable 所以你的临时变量中会有2
i guess you are using java so do like this 我猜你正在使用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
}
I already fixed the problem: So here's the Sql query: 我已经解决了这个问题:这是Sql查询:
select Violation, count(Violation), Date_Violated from violators group by Violation, Date_Violated 从“违规者”,“违规日期”,“违规者”组中选择“违规,计数(违规),Date_Violated”
Thank you guys for the help! 谢谢你们的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.