简体   繁体   中英

Get Current Month

This is sample my database:

  • status date

  • Q 2012-08-02.

  • Q 2012-08-01.
  • Q 2012-09-03.

Here if i run my application means i wish to display 2. This is do following information. - check the database current month+status=Q after display how many matched information is totally you are got.for example totally 2 means the output is displayed 2.

Already i done get the total count value for current date+status=Q information.it is successfully completed. Here i have use this code:

public class RetailerWs {
public int data(){
int count=0;

//count++;

try{
  Class.forName("com.mysql.jdbc.Driver");
  Connection con = DriverManager
     .getConnection("jdbc:mysql://localhost:3306/pro","root","");

  PreparedStatement statement =  con
     .prepareStatement("select * from orders where status='Q' AND date=CURDATE()");
  ResultSet result = statement.executeQuery();

  while(result.next()) {
    // Do something with the row returned.
    count++; //if the first col is a count.
  }     
}
catch (Exception exc) {
  System.out.println(exc.getMessage());
}


return count;
 }
 }

the another class is:

public class Demo {
public static void main(String[] args){
    RetailerWs obj = new RetailerWs();
    System.out.println(obj.data());
 }

}

the above code for get the information for current date + status=Q . please help me how is write the code for current month + status=Q ....

It should be:

select * from orders where status='Q' AND MONTH(date) = MONTH(CURDATE())

See THIS for more information on the date and time functions of MySQL.

For the week, try:

select * from orders where status='Q' AND WEEK(date) = WEEK(CURDATE())

For the week in THIS year, try:

select * from orders where status='Q' AND WEEK(date) = WEEK(CURDATE()) AND YEAR(date) = YEAR(CURDATE())

To have it all in one check use:

select * from orders where status='Q' AND YEARWEEK(date) = YEARWEEK(CURDATE())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM