簡體   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

我的表是這樣的,我必須在兩個日期之間獲取數據
從2011年11月11日至2014年3月1日

使用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

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]

要么

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

傳遞給查詢時,請確保[start date][end date]采用正確的日期格式。 如果這些參數是字符串參數,請對這些參數使用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