简体   繁体   English

仅在mysql中使用月和年从两个表中选择数据

[英]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 从2011年11月11日至2014年3月1日

Use MySQL functions STR_TO_DATE and CONCAT to put your month and year fields together as a date. 使用MySQL函数STR_TO_DATECONCAT将您的月和年字段放在一起作为日期。 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 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. 传递给查询时,请确保[start date][end date]采用正确的日期格式。 Use STR_TO_DATE on those parameters if they are string parameters. 如果这些参数是字符串参数,请对这些参数使用STR_TO_DATE

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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