1

i need to get the id and days count between days.

INSERT INTO Events
    (`ID`, `START`, `END`)
VALUES
    (2313, '2019-07-29', '2019-08-10'),
    (41, '2019-06-22', '2019-07-01'),
    (2540, '2019-06-22', '2019-07-02'),
    (2234, '2019-06-28', '2019-07-12'),
    (2634, '2019-06-30', '2019-07-04'),
    (53, '2019-06-30', '2019-07-02'),
    (1869, '2019-06-30', '2019-07-15'),
    (2132, '2019-07-30', '2019-08-10')
;

my query is

SELECT `ID`
  FROM Events 
 WHERE `START` BETWEEN '2019-07-01' AND '2019-07-31' 
   AND `END` BETWEEN '2019-07-01' AND '2019-07-31';

i need output as

2313 - 3
41 - 1
2540 - 2
2234 - 12
.....

please help me.

6
  • What is the second part 2313 - 3 (this 3 ??) Commented Aug 1, 2019 at 7:54
  • I might be wrong but no row satisfies the condition. Commented Aug 1, 2019 at 7:59
  • sorry, i need output as 2313 - 3, 41 - 1, 2540 - 2, 2234 - 12 Commented Aug 1, 2019 at 7:59
  • 1
    @YathavSriTechnology Second time from where comes 2313 - 3 (this 3 ??) Commented Aug 1, 2019 at 8:00
  • Second time from where comes 2313 - 3 is days count Commented Aug 1, 2019 at 8:01

2 Answers 2

4

GREATEST returns max value from list, LEAST - min

select ID, 
       DATEDIFF(LEAST(`END`,'2019-07-31'), GREATEST(`START`, '2019-07-01')) + 1
from events
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @splash58, i got exact output when i changed the date condition i get in-correct data. query is "select ID, DATEDIFF(LEAST(END,'2019-06-30'), GREATEST(START, '2019-06-01')) from events"
@YathavSriTechnology consider this
Finally i got the result by passing condition. "select ID, DATEDIFF(LEAST(END,'2019-08-31'), GREATEST(START, '2019-08-01')) + 1 as dDate from events having dDate > 0;"
@YathavSriTechnology Very well. I did not think about where condition :(
1

use DATEDIFF() function.

SELECT `id`, DATEDIFF(`start`, `end`) 
FROM `events` 
WHERE 
 `start` BETWEEN '2019-07-01' AND '2019-07-31' AND
 `end` BETWEEN '2019-07-01' AND '2019-07-31';

2 Comments

Unfortunately, this does not provide a solution for OP's question. Here's the Demo
when i change the date in query it showing negative value. i need output for between the date criteria not to show all the rows. dbfiddle.uk/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.