MySQL Get a list of dates in month, year

前端 未结 2 1175
面向向阳花
面向向阳花 2021-01-13 02:23

I am doing some reports and I want to get all dates in specific month, for example January 2014, and when I do this \'SQL\':

SELECT datesInMonth WHERE month          


        
相关标签:
2条回答
  • 2021-01-13 02:54

    You can use the datetime function in MySQL. In particular, MONTH() and YEAR(). If you pass a date into those functions, it'll return the month or year. For example:

    WHERE MONTH(my_date) = 2 and YEAR(my_date) = 2014
    

    If you want to get all of the dates in a month, you can do something like this:

    SELECT my_date FROM my_table
    WHERE MONTH(my_date) = 2 and YEAR(my_date) = 2014
    
    0 讨论(0)
  • 2021-01-13 02:58

    Here is the mysql/java solution for this question.

    CREATE TABLE STATMENT:

    CREATE TABLE `date_table` (
      `ID` bigint(20) NOT NULL AUTO_INCREMENT,
      `date` date NOT NULL,
      PRIMARY KEY (`ID`)
    ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1;
    

    Java code:

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
            Database db = new Database("root", "root01", "test-db", "10.10.5.11");
            db.connect();
            int start_year = 2013;
            int end_year = 2100;
            for (int year = start_year; year < end_year; year++) {
                for (int month = 1; month <= 12; month++) {
                    int days_in_month = 0;
                    switch (month) {
                        case 1: { //January
                            days_in_month = 31;
                            break;
                        }
                        case 2: { //February
                            if (year % 4 == 0) {
                                days_in_month = 29;
                            } else {
                                days_in_month = 28;
                            }
                            break;
                        }
                        case 3: { //March
                            days_in_month = 31;
                            break;
                        }
                        case 4: { //April
                            days_in_month = 30;
                            break;
                        }
                        case 5: { //May
                            days_in_month = 31;
                            break;
                        }
                        case 6: { //June
                            days_in_month = 30;
                            break;
                        }
                        case 7: { //July
                            days_in_month = 31;
                            break;
                        }
                        case 8: { //August
                            days_in_month = 31;
                            break;
                        }
                        case 9: { //September
                            days_in_month = 30;
                            break;
                        }
                        case 10: { //October
                            days_in_month = 31;
                            break;
                        }
                        case 11: { //November
                            days_in_month = 30;
                            break;
                        }
                        case 12: { //December
                            days_in_month = 31;
                            break;
                        }
                    }
                    for (int day = 1; day <= days_in_month; day++) {
                        db.executeInsert("INSERT INTO date_table (date) VALUES ('" + year + "-" + month + "-" + day + "');");
                    }
                }
                System.out.println("YEAR: " + year + " FINISHED");
            }
        }
    

    I get the list of dates using this sql:

    SELECT * FROM date_table WHERE YEAR(date) = 2014 AND MONTH(date) = 2;
    

    Hope someone use it :)

    0 讨论(0)
提交回复
热议问题