gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:21:43 25 4
gpt4 key购买 nike

所以我有一个名为“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com