java - 如何计算mysql中给定日期内某个项目出现的次数

标签 java mysql database phpmyadmin xampp

所以我有一个名为“violators”的表,并且有 2 列:“Violation”和“Date”。 违规者:

---------------------------
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

最佳答案

您的表格中将针对每条超速记录都有一个条目。

因此执行sql查询(例如使用php)

$resultset = select * from tablename where Violation ='overspeeding' And Date ='2016-05-05'

这会给你两条记录,所以只需在结果集上应用一个计数函数。在 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
}

关于java - 如何计算mysql中给定日期内某个项目出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38804805/

相关文章:

mysql - 如何结合SQL查询的where和order

mysql - 在执行之前记录查询

java - 为什么 XML ID 名称约定与 Java 的不同?

java - Spring 数据休息: NULL values issue

java - 小数或 double 之间的转换

java.lang.IllegalStateException : Fragment not attached to a context after API response

mysql - 使用exec.Command在Go中使用Mysql脚本

php - 无法在 Laravel 迁移中添加外键约束

java - 将 json 数据发送到 db 并在 listview 中显示抛出 "null pointer exception"

mysql - 如果绑定(bind)字段为空,如何使用左连接发出请求 |数据库