简体   繁体   English

我如何计算一个项目在给定日期在mysql中发生的次数

[英]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.

相关问题 如何计算字符串出现的次数 - How To Count The Number of Times a String Occurs 如何计算项目在列表中的次数 - How to count number of times an item is in a list 如何计算spring elasticsearch出现一个字段的次数? - How can I count the number of times a field occurred with spring elasticsearch? 一个数字出现多少次 - How many times a number occurs 给定日期范围列表,找到发生最多次数的日期 - Given a list of date ranges, find a date which occurs maximum times 给定一个数组,找到一个数字,使得一个数字的值等于它出现的次数。 对此我们有什么最佳解决方案? - Given an array find number such that the value of a number equals the number of times it occurs. What can we the most optimal solution for this? 我需要计算使用扫描仪和JFIleChooser在文件中出现子字符串的次数 - I need to count the number of times a substring occurs in a file using scanner and JFIleChooser 计算2D数组在给定列中元素出现的次数? - Counting the number of times an element occurs in a given column for a 2D array? 如何在数组中找到并打印在数组中恰好出现 K 次的最小数字,其中 K 是用户输入? - How can i find and print the smallest number in array which occurs exactly K times in an array, where K is an user input? 出现次数最多的数字…查找给定数字中出现次数最多的数字 - Count of most occurring digit… Find the digit that occurs most in a given number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM