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

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

Related

Sum monthly sales totals and provide output with month name and total for last 3 months

I am trying to get monthly sales totals from a MYSQL database and have them summed by month with month name for last X months. The two columns I am targeting are: total_customer_charged and local_time. I almost have my query working correctly, the problem I have is that the order the results come out sometimes changes with each query. Can anyone please give me some hints to get my query correct?
My existing query is:
select date_format(local_time,'%M'),sum(total_customer_charged) FROM ORDERS WHERE local_time BETWEEN curdate() - INTERVAL 3 MONTH AND curdate() group by month(local_time) order by year(local_time),month(local_time)
You should put year into the group-by statement like this:
select
date_format(local_time, '%Y') AS agg_year,
date_format(local_time, '%m') AS agg_month,
sum(total_customer_charged) AS monthly_total_customer_charged
FROM
ORDERS
WHERE
local_time BETWEEN curdate() - INTERVAL 3 MONTH AND curdate()
group by
date_format(local_time, '%Y'),
date_format(local_time, '%m')
order by
agg_year,
agg_month
SELECT MONTHNAME(local_time), SUM(total_customer_charged)
FROM ORDERS
GROUP BY YEAR(local_time), MONTH(local_time)

to retrieve total record id if datetime is greater than current date using query

I want to retrieve total record id if datetime is smaller than current date using myquery for each month.But I cann't retrieve it.I don't get any output .Date structure is datetime like that '2016-2-17 23:20:22'.So,how can I do that?
SELECT DATE_FORMAT('regist_timestamp','%Y-%m-%d') AS date, count(id) as numberOfVisits from ash_members
where DATE_FORMAT('regist_timestamp','%Y-%m-%d')<=curdate()
You need to group your count result. If you want monthly grouped result, you can do it this way.
SELECT MONTH(regist_timestamp) AS Month, YEAR(regist_timestamp) AS Year, count(id) AS numberOfVisits
FROM ash_members
WHERE DATE(regist_timestamp) < CURDATE()
GROUP BY MONTH(regist_timestamp), YEAR(regist_timestamp)

mysql php pdo select a part of row value date record

I want to select a part of a record in database. The record i want to select from is date.
The record date is this format 07/02/2015.
I want to have a select option where i can put option of year and month to used as parameter for the search.I want to know a select query for ff.
How can I select a specific year from the record(annual report)
I want to select all record in database with the current year, or with the year 2014.
How can i select a specific month from the record(monthly report)
I want to select all record in database with the current month and current year or with a X month and a X year
Use BETWEEN Keyword
SELECT * FROM tableName WHERE dateCol BETWEEN '01-01-2014' AND '31-12-2014';
for reference
http://www.w3schools.com/sql/sql_between.asp
Select * FROM table WHERE date between two selected dates
Please try
SELECT * FROM TABLENAME where YEAR(DATE_FORMAT(STR_TO_DATE(COLNAME, '%d/%m/%y'), '%Y-%m-%d')) = '2014'
SELECT * FROM TABLENAME where MONTH(DATE_FORMAT(STR_TO_DATE(COLNAME, '%d/%m/%y'), '%Y-%m-%d')) = x

how to simplify this statement? php mysql

i need to retrieve multiple selects from mysql table, my need is to simplify the statements
these are my statements
$stardate=dateselect
$enddate=dateselect
SELECT count(distinct(customer)) as customer FROM my_table where date_field BETWEEN'$startdate' AND '$enddate'
SELECT count(distinct(customer)) as computeris1 FROM my_table where computer=1 and date_field BETWEEN'$startdate' AND '$enddate'
SELECT count(distinct(customer)) as computeris2 FROM my_table where computer=2 and date_field BETWEEN'$startdate' AND '$enddate'
I have about 10 fields, each one has 4 or 5 values (choices) want to count the distinct customer for each field (e.g. computer) and all between 2 dates..
another problem is the date is a timestamp of mysql, doesn't return a correct and exclude today's records unless i select the end date as tomorrow then it works, i think it is a time issue, how i can make it count date correctly discarding the time?
You can fix the date/time problem by using date(). As for the rest, you can use conditional aggregation to do everything in one query:
SELECT count(distinct(customer)) as customer,
count(distinct case when computer = 1 then customer end) as computer1,
count(distinct case when computer = 2 then customer end) as computer2
FROM my_table
where date(date_field) BETWEEN '$startdate' AND '$enddate';
This puts the values as separate columns rather than separate rows.

How to sum up grouped results in SQL

I'm trying to avoid involving php after this query. I have a table that holds a list of employees and how much time they worked in seconds,date, etc... i want to:
select SUM(`seconds`) between date A and date B group by WEEK(`date`)
that will give me results for each week but now i want to get an average seconds worked per week by using AVG() on the whole result set. How could you accomplish this in one query?
You can use something like this
select sum(total) from (select SUM(`seconds`) as total between date A and date B group by WEEK(`date`)) as tbl1
Hope it help
This will do the trick:
Select AVG(sum_seconds) from (select SUM('seconds') as sum_seconds between date A and date B group by WEEK('date')) as a

Categories