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