Show all dates between, even if no result In group by - mysql

SELECT Count(Phq_2) as Phq_2, Date(created) as created
FROM `survey`
WHERE Phq_2 != ''
GROUP BY Date(created)
ORDER BY Date(created) ASC
Showing this
Expected Result

you can create a list of dates where you can use to list and join on your table. table c on the query below is the date table.
SELECT c.date,
COUNT(s.phq_2)
FROM (select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4
) c
LEFT JOIN `survey` s
ON c.date = s.created
WHERE s.phq_2 != ''
AND c.date BETWEEN '2017-08-30' AND '2017-09-06'
GROUP BY c.date
ORDER BY c.date
Result
date COUNT(s.phq_2)
2017-08-30 1
2017-08-31 1
2017-09-01 0
2017-09-02 0
2017-09-03 0
2017-09-04 0
2017-09-05 1
2017-09-06 6
if you want the dates fixed for WHERE c.date BETWEEN '2017-08-30' AND '2017-09-06', you can use the resulting MIN() and MAX() of column created from your table in a sub-query

Related

Fetch Column from second table using date from first table if the date is in between dates from second table

I have my first table here:
Then i have my second table here:
My problem is that, I needed to get the right corresponding ShiftAssignmentShiftID From second table if the selected_date from first table is in between the start date and end date of the second table. In short, I needed to get/display what shift of the employee is, per day. But I am getting all the ShiftAssignmentShiftID ALL as NULL values.
Here's my query:
SELECT FirstColumns.selected_date,FirstColumns.WeekDay,FirstColumns.EmployeeName,FirstColumns.EmployeeID, tblshiftassignments.ShiftAssignmentShiftID FROM (select EmployeeID,selected_date,CASE WHEN WEEKDAY(selected_date) = 0 THEN 'MON' WHEN WEEKDAY(selected_date) = 1 THEN 'TUES' WHEN WEEKDAY(selected_date) = 2 THEN 'WED' WHEN WEEKDAY(selected_date) = 3 THEN 'THU' WHEN WEEKDAY(selected_date) = 4 THEN 'FRI' WHEN WEEKDAY(selected_date) = 5 THEN 'SAT' WHEN WEEKDAY(selected_date) = 6 THEN 'SUN' END AS 'WeekDay', tblemployee.EmployeeIDDisplay, CONCAT(tblemployee.EmployeeLastName,',',tblemployee.EmployeeFirstName) AS 'EmployeeName' from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v,
tblemployee
where selected_date between '2021-08-01' and '2021-08-30'
order by tblemployee.EmployeeLastName, selected_date) AS FirstColumns LEFT JOIN tblshiftassignments ON tblshiftassignments.ShiftAssignmentEmployeeID = FirstColumns.EmployeeID AND tblshiftassignments.ShiftAssignmentStartDate <= FirstColumns.selected_date AND tblshiftassignments.ShiftAssignmentEndDate + INTERVAL 1 DAY > FirstColumns.selected_date
ORDER BY FirstColumns.EmployeeName,selected_date
Here's my desired result:
Solved! All my queries are correct. The dates that i was testing was 2021 but the data in the table is 2020, i missed this one out. That's why it's returning NULL.

Employee attendance calendar report design in iReport [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have different tables for track the employee attendance and holidays all things are done and working fine now I want to create a report for employee monthly calendar report facing some issues.
First thing is that
All tables are:
1. hr_employee
2. hr_biometric attendance
1. hr_employee_leave
1. hr_employee_visits
1. hr_holidays
1. Before 15 dates all Saturdays are count as working days after 15 dates all Saturdays are company holidays
2. In holiday records are in the date range from date and to date not single date holidays same for all other tables
I have created a query but it retrieves multiple records, and it's a hard query for me to write. How can I do this?
Here is query:
SET #fromDate = "2019-12-01";
SET #toDate = "2019-12-31";
SET #empId = 2814;
SET #recordStatus = "ATT";
SELECT
att.time_checked AS checkedTIme, #recordStatus
FROM
hr_employee emp
JOIN hr_biometric_attendance att ON att.`employee_id` = emp.id
WHERE emp.id = #empId
AND att.`time_checked` BETWEEN #fromDate AND #toDate
UNION
SELECT v.selected_date, "HD" FROM
(SELECT ADDDATE('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date FROM
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4) v
,
(SELECT hDay.date_from df, hDay.date_to dt FROM `hr_holiday` hDay WHERE hDay.date_from >= #fromDate AND hDay.date_to <= #toDate) hDayQ
WHERE selected_date BETWEEN hDayQ.df AND hDayQ.dt
UNION
SELECT v1.selected_date1, "EV" FROM
(SELECT ADDDATE('1970-01-01',t41.i*10000 + t31.i*1000 + t21.i*100 + t11.i*10 + t01.i) selected_date1 FROM
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t01,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t11,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t21,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t31,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t41) v1
,
(SELECT hEmpV.date_from df, hEmpV.date_to dt FROM `hr_employee_visit` hEmpV WHERE hEmpV.employee_id = #empId) hEmpVQ
WHERE selected_date1 BETWEEN hEmpVQ.df AND hEmpVQ.dt
UNION
SELECT v2.selected_date2, "EL" FROM
(SELECT ADDDATE('1970-01-01',t42.i*10000 + t32.i*1000 + t22.i*100 + t12.i*10 + t02.i) selected_date2 FROM
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t02,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t12,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t22,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t32,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t42) v2
,
(SELECT hEmpL.date_from df, hEmpL.date_to dt FROM `hr_employee_leave` hEmpL WHERE hEmpL.employee_id = #empId) hEmpLQ
WHERE selected_date2 BETWEEN hEmpLQ.df AND hEmpLQ.dt
UNION DISTINCT
SELECT v3.selected_date3, (SELECT IF(DAYNAME(v3.selected_date3) ='Sunday' OR (DAYOFMONTH(v3.selected_date3) >= 15 AND DAYNAME(v3.selected_date3) ='saturday'),'H','W'))
FROM
(SELECT ADDDATE('1970-01-01',t43.i*10000 + t33.i*1000 + t23.i*100 + t13.i*10 + t03.i) selected_date3 FROM
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t03,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t13,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t23,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t33,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t43) v3
WHERE selected_date3 BETWEEN #fromDate AND #toDate
ORDER BY `checkedTIme`;
Here is the erd tables:
Here is image for expected result report:
Here is Jasper result of above query:
Like I said in the comment, to simplify the query you should create a dedicated calendar table so you can shorten your query. Refer: https://www.db-fiddle.com/f/qVgmfDaVUszABAyFzH7iUS/0
Once that's done, you can re-write your query as following:
SET #fromDate = "2019-12-01";
SET #toDate = "2019-12-31";
SET #empId = 2814;
SET #recordStatus = "ATT";
SELECT att.time_checked AS checkedTIme, #recordStatus FROM hr_employee emp
JOIN hr_biometric_attendance att ON att.`employee_id` = emp.id
WHERE emp.id = #empId
AND att.`time_checked` BETWEEN #fromDate AND #toDate
UNION
SELECT v.selected_dates, "HD" FROM calendar v JOIN
(SELECT hDay.date_from df, hDay.date_to dt FROM `hr_holiday` hDay
WHERE hDay.date_from >= #fromDate AND hDay.date_to <= #toDate) hDayQ
ON selected_dates BETWEEN hDayQ.df AND hDayQ.dt
UNION
SELECT v1.selected_dates, "EV" FROM calendar v1 JOIN
(SELECT hEmpV.date_from df, hEmpV.date_to dt FROM `hr_employee_visit` hEmpV
WHERE hEmpV.employee_id = #empId) hEmpVQ
ON selected_dates BETWEEN hEmpVQ.df AND hEmpVQ.dt
UNION
SELECT v2.selected_dates, "EL" FROM calendar v2 JOIN
(SELECT hEmpL.date_from df, hEmpL.date_to dt FROM `hr_employee_leave` hEmpL
WHERE hEmpL.employee_id = #empId) hEmpLQ
ON selected_dates BETWEEN hEmpLQ.df AND hEmpLQ.dt
UNION DISTINCT
SELECT v3.selected_dates, (SELECT IF(DAYNAME(v3.selected_dates) ='Sunday'
OR (DAYOFMONTH(v3.selected_dates) >= 15 AND DAYNAME(v3.selected_dates) ='saturday'),'H','W'))
FROM calendar v3
WHERE selected_dates BETWEEN #fromDate AND #toDate
ORDER BY `checkedTIme`;
Now it's much easier to read then you can start with building your final query.

How to Get List of weeks between the given date range?

I want week list with week number, week start date and end date between two dates. Let me give you an example,
If I am passing start date as 2019-12-11 and end date as 2019-12-25, then expected output should look like below:
Week Number | Week start date | Week end date
W1 2019-12-11 2019-12-14
W2 2019-12-15 2019-12-21
W3 2019-12-22 2019-12-25
I have tried using below query but I got output like
Week start date | Week end date
2019-12-15 2019-12-21
2019-12-22 2019-12-28
select start_date, date_add(start_date, INTERVAL 6 DAY) as end_date
from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) start_date
from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where start_date between '2019-12-11' and '2019-12-25'
AND date_format(start_date, '%w') = 0
You can use the following solution using WEEK function and ROW_NUMBER:
-- calendar table: https://stackoverflow.com/a/45992247/3840840
SELECT CONCAT('W', ROW_NUMBER() OVER (ORDER BY WEEK(start_date))) AS `Week Number`,
MIN(start_date) AS `Week start date`, MAX(start_date) AS `Week end date`
FROM (
SELECT ADDDATE('1970-01-01', t4 * 10000 + t3 * 1000 + t2 * 100 + t1 * 10 + t0) AS start_date
FROM
(SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4
) v
WHERE start_date BETWEEN '2019-12-11' AND '2019-12-25'
GROUP BY WEEK(start_date)
ORDER BY WEEK(start_date)
By using WEEK you don't have to calculate the weeks yourself. MySQL can detect the weeks. With WEEK you can get the week number of a date value. You can group by this week number to get the rows for every week. With MIN and MAX on the date value you get the first and last date of each week.
With ROW_NUMBER you can add a row number to your result. This function is available since MySQL 8.0. On earlier versions of MySQL you can use a solution like the following:
SELECT CONCAT('W', #row_number:=#row_number+1) AS `Week Number`,
MIN(start_date) AS `Week start date`, MAX(start_date) AS `Week end date`
FROM (
SELECT ADDDATE('1970-01-01', t4 * 10000 + t3 * 1000 + t2 * 100 + t1 * 10 + t0) AS start_date
FROM
(SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4
ORDER BY start_date
) v, (SELECT #row_number:=0) rn
WHERE start_date BETWEEN '2019-12-11' AND '2019-12-25'
GROUP BY WEEK(start_date)
ORDER BY WEEK(start_date)
demo on dbfiddle.uk

MySQL query to get row count when no data available, skip result when add where clause

My following query showing data count even when there is no data for that date.
SELECT c.date AS DATE, COUNT(s.insertTime) AS tot_cnt FROM (SELECT ADDDATE('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i)
DATE FROM (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4 ) c
LEFT JOIN `access_log` s ON c.date = DATE(s.insertTime)
WHERE UNIX_TIMESTAMP(c.date) BETWEEN 1563733801 AND 1564338599 GROUP BY c.date ORDER BY c.date;
But when I add userId=8, it skips all date range which don't have any value
SELECT c.date AS DATE, COUNT(s.insertTime) AS tot_cnt FROM (SELECT ADDDATE('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i)
DATE FROM (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4 ) c
LEFT JOIN `access_log` s ON c.date = DATE(s.insertTime)
WHERE UNIX_TIMESTAMP(c.date) BETWEEN 1563733801 AND 1564338599 AND userId=8 GROUP BY c.date ORDER BY c.date;
I need all date even they don't have value and also when apply userId=8 to filter records.
Apply the condition in the ON clause and not the WHERE clause:
ON c.date = DATE(s.insertTime) AND userId=8
so the LEFT JOIN returns the non matching rows.

mysql: Row count by date in between

From the screenshot shown, it is failed/missing to show the count of date
'2018-06-23' ,2018-06-25,2018-06-26,2018-06-27 , can you help me to write the correct query so that i get count of missing dates. I am trying to get count of total number of rows on particular date(created_date).
SELECT count(book_id) as count,created_date as cdate
FROM `bookdetails`
WHERE (created_date >= '2018-06-21' AND created_date <='2018-06-27')
GROUP by DATE(created_date)
ORDER BY MIN(created_date)
Expected output:
count | cdate
----------------------
98 2018-06-21
39 2018-06-22
0 2018-06-23 //because no data
39 2018-06-24
XX 2018-06-25
XX 2018-06-26
XX 2018-06-27
Table: bookdetails structure
Date generation and join need to show date that doest exist
select count(book_id) as count,date(C_date) as cdate from
(
select * from
(select adddate('1970-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) C_date from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where C_date between '2018-01-01' and '2018-12-31'
) All_date
left join bookdetails
on date(created_date)=All_date.C_date
where ( date(All_date.C_date) >= '2018-06-21' AND date(All_date.C_date) <='2018-06-27')
group by date(C_date)
order by date(C_date) asc
Why not simply use BETWEEN instead of => and <=.
Usage:
SELECT * FROM bookdetails
WHERE created_date BETWEEN lowerdate AND upperdate
Show us your backend. We'll be able to assist you better.
And what is the expected result?