简体   繁体   中英

select data from two table using month and year only in mysql

Entered-month  Entered-year  Amount
-------------------------------------
01             2013        3000
02             2013        4000
.               .           .
.               .           .
.               .           .
01             2014        2000

My table is like this I have to take data Amounts between Two dates
For ex 01/11/2011 To 03/01/2014

Use MySQL functions STR_TO_DATE and CONCAT to put your month and year fields together as a date. http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_str-to-date http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_concat

select Amount
from tbl1
where STR_TO_DATE(CONCAT(tbl1.Entered-month,'-',tbl1.Entered-year),'%m-%Y') >= [start date] 
    and STR_TO_DATE(CONCAT(tbl1.Entered-month,'-',tbl1.Entered-year),'%m-%Y') <= [end date]

OR

select Amount
from tbl1
where STR_TO_DATE(CONCAT(tbl1.Entered-month,'-',tbl1.Entered-year),'%m-%Y') 
    between [start date] and [end date]

Make sure [start date] and [end date] are in the correct date format when passed into the query. Use STR_TO_DATE on those parameters if they are string parameters.

select Amount
from tbl1
where STR_TO_DATE(CONCAT(tbl1.Entered-month,'-',tbl1.Entered-year),'%m-%Y') 
    between STR_TO_DATE(startDate,'%Y-%m-%d') and STR_TO_DATE(endDate,'%Y-%m-%d')

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