mysql php pdo select a part of row value date record

点点圈 提交于 2019-12-25 02:59:43

问题


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.

  1. 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.

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!