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 8071993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:52:10+00:00 2026-06-05T13:52:10+00:00

I have two tables: Events (ID, Name, Time, Recorder) Video (ID, StartTime, EndTime, Recorder,

  • 0

I have two tables:

Events (ID, Name, Time, Recorder)
Video (ID, StartTime, EndTime, Recorder, Filename)

I wish to join the Event data to the video data, so that for every event I get the video filename. The recorder field is used to specify which recorder was operable at the event’s time, and assists with multiple recorders recording video at the same time.

If i wasn’t concerned about the events which have no video then this is fine (I can get the SQL), however in my case i wish to show the closest video filename and the seconds difference.

EDIT

Sample Data

Events

1, EV1, 2012-01-01 12:00, A
2, EV2, 2012-01-01 13:00, B
3, EV3, 2012-01-01 12:15, B
4, EV4, 2012-01-01 11:45, A

Video

1, 2012-01-01 12:00, 2012-01-01 12:30, A, 1.mpg
2, 2012-01-01 13:00, 2012-01-01 13:30, A, 2.mpg 
3, 2012-01-01 12:00, 2012-01-01 12:30, B, 3.mpg

Result
(EventID, VideoID, Filename, IsBetween, SecondsDifference)

1, 1, 1.mpg, TRUE, 0
2, 3, 3.mpg, FALSE, 1800 //1800 seconds from the end of video 3
3, 3, 3.mpg, TRUE, 900
4, 1, 1.mpg, FALSE, 900  //900 seconds from the start of video 1 

BONUS

I would be even nicer if the closest video did not take the recorder into account (but the first bounds (Start and End) check to take it into account) If this is too difficult then thats fine.

  • 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-05T13:52:12+00:00Added an answer on June 5, 2026 at 1:52 pm

    It’s a little clunky, but here’s what I came up with:

    SELECT
        *
    FROM
        (
            SELECT
                a.ID AS EventID,
                b.ID AS VideoID,
                b.Filename,
                (
                    CASE
                        WHEN a.Time < b.StartTime THEN UNIX_TIMESTAMP(b.StartTime) - UNIX_TIMESTAMP(a.Time)
                        WHEN a.Time > b.EndTime THEN UNIX_TIMESTAMP(a.Time) - UNIX_TIMESTAMP(b.EndTime)
                    END
                ) AS distance_factor
            FROM
                `Events` a
            CROSS JOIN
                video b
            WHERE
                NOT EXISTS
                (
                    SELECT NULL
                    FROM Video
                    WHERE a.Time BETWEEN StartTime AND EndTime
                )
        ) c
    WHERE 
        c.distance_factor = 
        (
            SELECT
                MIN(CASE WHEN d.Time < e.StartTime THEN UNIX_TIMESTAMP(e.StartTime) - UNIX_TIMESTAMP(d.Time) WHEN d.Time > e.EndTime THEN UNIX_TIMESTAMP(d.Time) - UNIX_TIMESTAMP(e.EndTime) END)
            FROM
                `Events` d
            CROSS JOIN
                video e
            WHERE d.ID = c.EventID
        )
    GROUP BY
        c.EventID
    

    This returns events whose dates don’t fall between any of the time ranges of any video, but then returns the video that falls the closest to that event date.

    The only thing right now is that there are some videos where the seconds difference is exactly the same. I don’t know if you want it to return 2 rows, but for now, I put in the GROUP BY to just select one.

    Let me know how that works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables like so: EventTable (EID, Name, EventTime) VideoTable (VID, StartTime, Video,
I have got two tables: Event +----------+---------+--------------+ | event_id | name | date |
I have two msyql tables, Badges and Events. I use a join to find
I have two tables: events : id, name, date_from, date_to event_recurrences : id, event_id,
I have two entities: Event and Comment. @Entity @Table(name = events) public class Event
I have two tables: events and events_actions events columns: event_id user_id event_name events_actions columns:
I have two tables, events and photos, which relate together via the 'Event_ID' column.
I have two entities: User and Event. Each user has a list of events
i have two tables, program and event. what i am trying to do is
I have two tables: candidate_register_table with the following schema: |----------------------------------------------------| | username | name

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.