Question

I have following table

  id      date  
------- + ---------      
  1      10-01-2014  
  2      10-01-2014  
  3      15-01-2014  
  4      15-01-2014  
  5      20-01-2014  
  6      30-01-2014  
  7      03-02-2014  
  8      12-02-2014  
  9      12-02-2014 
  10     19-02-2014  
  11     19-02-2014  

My table name is : attendance . I want to calculate the number of dates in Between 01-01-2014 to 20-02-2014 without repetiton.

Following is my query for this :

SELECT distinct(todaydate) FROM attendance WHERE  todaydate between '01-02-2014' and '24-02-2014'

its working but the answer is wrong. Any solution. Thanks in advance.

Was it helpful?

Solution

Try this

SELECT DISTINCT(`date`) AS date
FROM attendance
WHERE UNIX_TIMESTAMP(`date`)>UNIX_TIMESTAMP('01-01-2014') 
      AND
      UNIX_TIMESTAMP(`date`)<UNIX_TIMESTAMP('24-02-2014')  

OR

SELECT date
FROM attendance
WHERE UNIX_TIMESTAMP(`date`)>UNIX_TIMESTAMP('01-01-2014') 
      AND
      UNIX_TIMESTAMP(`date`)<UNIX_TIMESTAMP('24-02-2014')  
GROUP BY date

OTHER TIPS

In question you want to calculate dates in between 01-01-2014 to 20-02-2014 but in query you have mentioned some other date.

So actual query will be:

SELECT distinct(todaydate) 
FROM attendance 
WHERE  todaydate between '01-01-2014' and '24-02-2014'

Your dates are string you need to convert them into MySQL Date using STR_TO_DATE

Try like

SELECT distinct
    (STR_TO_DATE(todaydate, '%d-%m-%Y')) as todaydate
FROM
    attendance
WHERE
    todaydate between STR_TO_DATE('01-02-2014', '%d-%m-%Y') 
AND STR_TO_DATE('24-02-2014', '%d-%m-%Y');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top