Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9149355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:27:57+00:00 2026-06-17T11:27:57+00:00

I want to count N consecutive days that a specific user has meetings, on

  • 0

I want to count N consecutive days that a specific user has meetings, on a given date and before it.

For example: count the consecutive meeting days that a user with id 1 has at 16 January 2013.

I found some good answers here and here but the tables are not in normal form like my sample above and i cannot figure out how to implement it for my occasion.

A sample table structure as follows:

CREATE TABLE IF NOT EXISTS `meetings` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `time` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `meetings_users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(10) unsigned NOT NULL,
  `meeting_id` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  KEY `meeting_id` (`meeting_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

--
-- Constraints for table `meetings_users`
--
ALTER TABLE `meetings_users`
  ADD CONSTRAINT `meetings_users_ibfk_2` FOREIGN KEY (`meeting_id`) REFERENCES `meetings` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `meetings_users_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

Sample inserts

INSERT INTO  `users` ( `id` ) VALUES (1)

INSERT INTO `meetings` ( `id`, `time` ) VALUES 
(1, '2013-01-14 10:00:00'), 
(2, '2013-01-15 10:00:00'), 
(3, '2013-01-16 10:00:00')


INSERT INTO `meetings_users` ( `id`, `meeting_id`, `user_id` ) VALUES 
(1, 1, 1), 
(2, 2, 1), 
(3, 3, 1)

Desired output:

*+---------+-----------------+
| user_id | consecutive_days |
+---------+------------------+
|       1 | 3                |
+---------+------------------+
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:27:57+00:00Added an answer on June 17, 2026 at 11:27 am

    How about something like this. I expect it can be re-written without the subqueries but I must be having a bit of a brain freeze… (data set and query amended to suit shifting requirements)

    DROP TABLE IF EXISTS meetings;
    CREATE TABLE IF NOT EXISTS meetings 
    ( meeting_id int(10) unsigned NOT NULL AUTO_INCREMENT
    , meeting_time datetime NOT NULL
    , PRIMARY KEY (meeting_id)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
    
    DROP TABLE IF EXISTS meetings_users;
    CREATE TABLE IF NOT EXISTS meetings_users 
    ( user_id int(10) unsigned NOT NULL
    , meeting_id int(10) unsigned NOT NULL
    , PRIMARY KEY (meeting_id,user_id)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
    
    DROP TABLE IF EXISTS users;
    CREATE TABLE IF NOT EXISTS users 
    ( user_id int(10) unsigned NOT NULL AUTO_INCREMENT
    , PRIMARY KEY (user_id)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;
    
    INSERT INTO  users ( user_id ) VALUES (1),(2),(3),(4);
    
    INSERT INTO meetings ( meeting_id, meeting_time ) VALUES 
    (1, '2013-01-14 10:00:00'), 
    (2, '2013-01-15 10:00:00'), 
    (3, '2013-01-16 10:00:00'),
    (4, '2013-01-17 10:00:00'),
    (5, '2013-01-18 10:00:00'),
    (6, '2013-01-19 10:00:00'),
    (7, '2013-01-20 10:00:00'),
    (8, '2013-01-14 12:00:00');
    
    
    INSERT INTO meetings_users (meeting_id, user_id ) VALUES 
    (1, 1), 
    (2, 1),
    (2, 3),
    (3, 1),
    (3, 3),
    (4, 2),
    (4, 3), 
    (5, 2), 
    (6, 1),
    (1, 8);
    
    SET @dt = '2013-01-15';
    
    SELECT user_id
         , start
         , DATEDIFF(@dt,start)+1 cons
      FROM
         (
           SELECT a.user_id
                , a.meeting_date Start
                , MIN(c.meeting_date) End
            , DATEDIFF(MIN(c.meeting_date),a.meeting_date)  + 1 diff
             FROM (SELECT DISTINCT mu.user_id,DATE(m.meeting_time) meeting_date FROM meetings_users mu JOIN meetings m ON m.meeting_id = mu.meeting_id) a
             LEFT
             JOIN (SELECT DISTINCT mu.user_id,DATE(m.meeting_time) meeting_date FROM meetings_users mu JOIN meetings m ON m.meeting_id = mu.meeting_id) b
           ON b.user_id = a.user_id
              AND a.meeting_date = b.meeting_date + INTERVAL 1 DAY
             LEFT
             JOIN (SELECT DISTINCT mu.user_id,DATE(m.meeting_time) meeting_date FROM meetings_users mu JOIN meetings m ON m.meeting_id = mu.meeting_id) c
           ON c.user_id = a.user_id
              AND a.meeting_date <= c.meeting_date
             LEFT
             JOIN (SELECT DISTINCT mu.user_id,DATE(m.meeting_time) meeting_date FROM meetings_users mu JOIN meetings m ON m.meeting_id = mu.meeting_id) d
               ON d.user_id = a.user_id
              AND c.meeting_date = d.meeting_date - INTERVAL 1 DAY
            WHERE b.meeting_date IS NULL
          AND c.meeting_date IS NOT NULL
              AND d.meeting_date IS NULL
            GROUP
           BY a.user_id
            , a.meeting_date
         ) x
     WHERE @dt BETWEEN start AND end;
     +---------+------------+------+
     | user_id | start      | cons |
     +---------+------------+------+
     |       1 | 2013-01-14 |    2 |
     |       3 | 2013-01-15 |    1 |
     +---------+------------+------+ 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many checkboxes a user has selected. For example, from
I want to count how many cells that have a value given that a
I want to count the total day difference from user input For example when
We want to count number of shakes done by user. We tried motionBegan, motionEnded
I want to count the rows in several date ranges (i.e: last hour, today,
I want to count how many members of an iterable meet a given condition.
I want to count tokens in word which don't duplicate . Example: aabbcc abc
I want to count number of occurence of BB code like word (example: [b]
I want to Count all the rows that only have the value I want
I want count for example adverbs, but there are different tags for different types,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.