title: I want to calculate time between 2 Dates of a day with multiple rows in mysql
body: <p>I'm using this query to calculate the login time of a user on the app for the whole day and previous 5 days</p>
  
  <pre><code>Select 
    sec_to_time(sum(time_to_sec(TIMEDIFF((IFNULL(logoff_time, ADDTIME(now(), '05:00:00'))),login_time)))) as online_time 
  from tb_sessions  
  WHERE 
    (login_time BETWEEN DATE(DATE_ADD(now(), INTERVAL (-6) DAY)) 
  AND
    ADDTIME(now(), '5:00:00')) AND user_id = 30982
  AND TIME(`login_time`) between "00:00:00" AND "23:59:59"
  group by DATE(login_time)
  </code></pre>
  
  <p>Now i have some new requirements:</p>
  
  <p><strong>Calculate time from 07:00:00 to 23:59:59</strong></p>
  
  <p>My Table: <strong>tb_sessions</strong></p>
  
  <pre><code>id |   user_id   |       login_time      |   logoff_time
  
  1        3098       2017-06-10 06:30:00    2017-06-10 07:45:00
  2        3098       2017-06-10 07:45:01    2017-06-10 08:30:00
  </code></pre>
  
  <p>By using above query total oline time is = 02:00:00 
  But i want only time from 7:00 to 8:30, so total time will be = 1:30:00
  I make some changes in query with cases but no success.</p>
  
  <p>You can check my query on the below link:</p>
  
  <blockquote>
    <p><a href="http://sqlfiddle.com/#!9/4620af/12" rel="nofollow noreferrer">http://sqlfiddle.com/#!9/4620af/12</a></p>
  </blockquote>