简体   繁体   中英

how to fetch exact data from mysql table in month date condition

hello everyone for my erp system i want some specific type of data fetch from database for example if i want number of row added in current month purchase then i use

select count(receive_no) FROM tbl where contact_id=".$contactid." AND MONTH(created_on) = MONTH(CURDATE());

but it gets false results and if i want sum of result in current month then i use

select sum(price) FROM tbl where contact_id=".$contactid." AND MONTH(created_on) = MONTH(CURDATE())

and if i want today date number of row or purchase then i use

select count(receive_no) FROM tbl where contact_id=".$contactid." AND created_on >= CURDATE()

so as if i want today sum of purchase then i use

select sum(price) FROM tbl where contact_id=".$contactid." AND created_on >= CURDATE()

my table created on field datetime like 2016-09-01 11:56:45 so please suggest me better query to fetch exact data thanks

It's not really clear what - if any - the problem is with the last 3 queries, but the first one will get you the data from this month this year and this month last year, the year before that, etc. etc.

So what you need is something like (there are several ways to write that...):

select count(receive_no) FROM tbl where 
        contact_id=".$contactid." 
    AND MONTH(created_on) = MONTH(CURDATE())
    AND YEAR(created_on) = YEAR(CURDATE())

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