简体   繁体   English

如何在月日期条件下从mysql表中获取精确数据

[英]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 大家好我的erp系统我希望从数据库中获取一些特定类型的数据,例如,如果我想在当前月份购买时添加行数,那么我使用

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 我的表在字段日期时间创建,如2016-09-01 11:56:45所以请建议我更好的查询以获取确切的数据谢谢

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. 目前还不清楚 - 如果有的话 - 最后3个问题是什么问题,但是第一个问题会得到今年去年这个月,前一年等的数据,等等。

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

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

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