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

  • SEARCH
  • Home
  • 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 7653803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:06:57+00:00 2026-05-31T12:06:57+00:00

I have a table availablities which holds availabilities of users: row_id | user_id |

  • 0

I have a table availablities which holds availabilities of users:

row_id | user_id  | available_from      | available_to
-------------------------------------------------------------
1      | 1        | 2012-02-01 08:00:00 | 2012-02-01 09:00:00
2      | 1        | 2012-02-01 09:00:00 | 2012-02-01 10:00:00
3      | 2        | 2012-02-01 08:00:00 | 2012-02-01 10:00:00
4      | 3        | 2012-02-01 07:00:00 | 2012-02-01 12:00:00

I need to get all available users from this table for a certain event.

Scenario: get all available users for an event that starts 2012-02-01 08:00:00 and ends 2012-02-01 10:00:00.

I have no problem getting user_ids (2,3):

SELECT `user_id` FROM `availablities`
WHERE `available_from` <= "2012-02-01 08:00:00"
AND `available_to` >= "2012-02-01 10:00:00"

I am having a hard time though to find a query that will also return user_id (1). For that the query must somehown combine the two rows (1) and (2) of user_id (1) because only the sum of those two availabilities fits the event.

In a perfect world the adjacent availabilities on the rows (1,2) would be in just one row. There are reasons why it is saved this ways and I can’t just “optimize” the data in the tables to combine adjacent availabilities in one row.

So what I need is a way in mysql to return user_ids (1,2,3) in one query for the given timeframe – possible or do I have to find another approach?

  • 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-05-31T12:06:59+00:00Added an answer on May 31, 2026 at 12:06 pm

    If you need to do this in one query you could try combining MySQL User Defined variables and some inline view trickery:

    select  d.*
    from
    (
    select u.user_id,u.consec as available_from,max(u.available_to) as available_to
    from
    (
    select a.*,
    case when @lastEndDate != a.available_from then @curStartDate := a.available_from else @curStartDate end as consec,
    @lastEndDate := a.available_to as c
    from availabilities a
    inner join (select @curStartDate := null,@lastEndDate := "1970-01-01 00:00:00") as t
    order by a.user_id,a.available_from
    ) u
    group by u.user_id,u.consec
    ) d
    where d.available_from <= "2012-02-01 08:00:00"
    AND d.available_to >= "2012-02-01 10:00:00";
    

    I took your test data on a bit and included a user (user_id = 4) who was available between 2012-02-01 08:00:00 and 2012-02-01 08:30:00 and then again between 2012-02-01 09:00:00 and 2012-02-01 10:00:00. So he was not available for the entire period between 2012-02-01 08:00:00 and 2012-02-01 10:00:00 (coffee break?!).

    So the expected result is that user_id = 1 is returned for the time period but user_id =4 is not.

    Here is the test I used:

    drop table if exists availabilities;
    
    create table availabilities
    (row_id integer unsigned not null primary key,
    user_id integer not null,
    available_from datetime not null,
    available_to datetime not null
    );
    
    insert into availabilities values (1,1,'2012-02-01 08:00:00','2012-02-01 09:00:00');
    insert into availabilities values (2,1,'2012-02-01 09:00:00','2012-02-01 10:00:00');
    insert into availabilities values (3,2,'2012-02-01 08:00:00','2012-02-01 10:00:00');
    insert into availabilities values (4,3,'2012-02-01 07:00:00','2012-02-01 12:00:00');
    insert into availabilities values (5,4,'2012-02-01 08:00:00','2012-02-01 08:30:00');
    insert into availabilities values (6,4,'2012-02-01 09:00:00','2012-02-01 10:00:00');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have table, which is described in main xml layout. I need to know,
I have table A in Oracle that has a primary key (id). I need
I have table 'products' and I need to update 'price' field by 20% if
I have table defined with FlexiGrid. Call to all variables is ok. One column
I have table in my database which has fields of ID,NAME,CONTEXT. I am showing
I have table view which is populated from an array grabbed from a url.
I have table like below ID User Date Location 1 Tom 6-Mar-2012 US 2
I have table A(id). I need to create table B(id) add a foreign key
I have table view which is showing list of files to download. When I
I have table ,and i need access them from shell script and do the

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.