问题
I want to select a part of a record in database. The record i want to select from is date.
The record date is this format 07/02/2015.
I want to have a select option where i can put option of year and month to used as parameter for the search.I want to know a select query for ff.
How can I select a specific year from the record(annual report)
I want to select all record in database with the current year, or with the year 2014.
How can i select a specific month from the record(monthly report)
I want to select all record in database with the current month and current year or with a X month and a X year
回答1:
Use BETWEEN Keyword
SELECT * FROM tableName WHERE dateCol BETWEEN '01-01-2014' AND '31-12-2014';
for reference http://www.w3schools.com/sql/sql_between.asp
Select * FROM table WHERE date between two selected dates
回答2:
Please try
SELECT * FROM TABLENAME where YEAR(DATE_FORMAT(STR_TO_DATE(COLNAME, '%d/%m/%y'), '%Y-%m-%d')) = '2014'
SELECT * FROM TABLENAME where MONTH(DATE_FORMAT(STR_TO_DATE(COLNAME, '%d/%m/%y'), '%Y-%m-%d')) = x
来源:https://stackoverflow.com/questions/31204869/mysql-php-pdo-select-a-part-of-row-value-date-record