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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:30:18+00:00 2026-06-11T21:30:18+00:00

I have five tables in MySQL which I am generating some reports from. I

  • 0

I have five tables in MySQL which I am generating some reports from. I will abstract the table names slightly for display purposes. Let’s assume the tables I am working with are as described below:

History  (id, serial, date, time) : Stores historic data, this is the main
                                    column we query.
Serial   (id, serial)             : Stores serial numbers used in the next
                                    3 tables.
Employee (id, serial_id, name)
Event    (id, serial_id, name)
Location (id, serial_id, name)

EDIT: Rewrote the whole question in hopes that it is clearer and easier to understand…

I need to query for events, also if an employee serial is found in history directly PRIOR to a row with a serial matching an event, that employee will be associated with that event, likewise, if a location is found in the history directly AFTER the row with a serial matching an event, that location will be associated with that event.

Sample data:

History:
+------+------------+------------+----------+
| id   | serial     | date       | time     |
+------+------------+------------+----------+
| 1    | 00001234   | 2012-09-25 | 09:00:00 |
+------+------------+------------+----------+
| 2    | 00001235   | 2012-09-25 | 09:01:00 |
+------+------------+------------+----------+
| 3    | 00001236   | 2012-09-25 | 09:02:00 |
+------+------------+------------+----------+
| 4    | 00001235   | 2012-09-25 | 09:05:00 |
+------+------------+------------+----------+
| 5    | 00001236   | 2012-09-25 | 09:07:00 |
+------+------------+------------+----------+
| 6    | 00001235   | 2012-09-25 | 09:10:00 |
+------+------------+------------+----------+
| 7    | 00001235   | 2012-09-25 | 09:11:00 |
+------+------------+------------+----------+
| 8    | 00001235   | 2012-09-25 | 09:12:00 |
+------+------------+------------+----------+
| 9    | 00001236   | 2012-09-25 | 09:15:00 |
+------+------------+------------+----------+
| 10   | 00001234   | 2012-09-25 | 09:20:00 |
+------+------------+------------+----------+
| 11   | 00001236   | 2012-09-25 | 09:25:00 |
+------+------------+------------+----------+

Serial:
+----------+------------+
| id       | serial     |
+----------+------------+
| 1        | 00001234   |
+----------+------------+
| 2        | 00001235   |
+----------+------------+
| 3        | 00001236   |
+----------+------------+

Employee:
+----------+------------+-------------+
| id       | serial_id  | name        |
+----------+------------+-------------+
| 1        | 1          | John Smith  |
+----------+------------+-------------+

Event:
+----------+------------+-------------+
| id       | serial_id  | name        |
+----------+------------+-------------+
| 1        | 2          | Event 1     |
+----------+------------+-------------+

Location:
+----------+------------+-------------+
| id       | serial_id  | name        |
+----------+------------+-------------+
| 1        | 3          | Location 1  |
+----------+------------+-------------+

If I were to do a normal LEFT JOIN query (just for clarity as the difference in results), with the following query, results would be:

SELECT
    history.id,
    history.serial,
    history.date,
    history.time,
    employee.name as 'employee',
    event.name as 'event',
    location.name as 'location'
FROM history
LEFT JOIN serial ON history.serial = serial.serial
LEFT JOIN employee ON serial.id = employee.serial_id
LEFT JOIN event ON serial.id = event.serial_id
LEFT JOIN location ON location.id = location.serial_id
ORDER BY date, time

Results on normal LEFT JOIN are (for clarity only, I don’t need this result set):

+----+----------+------------+----------+------------+----------+------------+
| id | serial   | date       | time     | employee   | event    | location   |
+----+----------+------------+----------+------------+----------+------------+
| 1  | 00001234 | 2012-09-25 | 09:00:00 | John Smith | NULL     | NULL       |
+----+----------+------------+----------+------------+----------+------------+
| 2  | 00001235 | 2012-09-25 | 09:01:00 | NULL       | Event 1  | NULL       |
+----+----------+------------+----------+------------+----------+------------+
| 3  | 00001236 | 2012-09-25 | 09:02:00 | NULL       | NULL     | Location 1 |
+----+----------+------------+----------+------------+----------+------------+
| 4  | 00001235 | 2012-09-25 | 09:05:00 | NULL       | Event 1  | NULL       |
+----+----------+------------+----------+------------+----------+------------+
| 5  | 00001236 | 2012-09-25 | 09:07:00 | NULL       | NULL     | Location 1 |
+----+----------+------------+----------+------------+----------+------------+
| 6  | 00001235 | 2012-09-25 | 09:10:00 | NULL       | Event 1  | NULL       |
+----+----------+------------+----------+------------+----------+------------+
| 7  | 00001235 | 2012-09-25 | 09:11:00 | NULL       | Event 1  | NULL       |
+----+----------+------------+----------+------------+----------+------------+
| 8  | 00001235 | 2012-09-25 | 09:12:00 | NULL       | Event 1  | NULL       |
+----+----------+------------+----------+------------+----------+------------+
| 9  | 00001236 | 2012-09-25 | 09:15:00 | NULL       | NULL     | Location 1 |
+-----+---------+------------+----------+------------+----------+------------+
| 10 | 00001234 | 2012-09-25 | 09:20:00 | John Smith | NULL     | NULL       |
+----+----------+------------+----------+------------+----------+------------+
| 11 | 00001236 | 2012-09-25 | 09:25:00 | NULL       | NULL     | Location 1 |
+----+----------+------------+----------+------------+----------+------------+

The query should be based off the event, event should never be NULL. It should look at the row PRIOR and label it as employee (or null if no match), it should look at the row AFTER and label it as location (or null if no match). Example of what the result should be, using the above data:

Result should be:
+----------+------------+------------+------------+----------+------------+
| e_serial | date       | time       | employee   | event    | location   |
+----------+------------+------------+------------+----------+------------+
| 00001235 | 2012-09-25 | 09:01:00   | John Smith | Event 1  | Location 1 |
+----------+------------+------------+------------+----------+------------+
| 00001235 | 2012-09-25 | 09:05:00   | NULL       | Event 1  | Location 1 |
+----------+------------+------------+------------+----------+------------+
| 00001235 | 2012-09-25 | 09:10:00   | NULL       | Event 1  | NULL       |
+----------+------------+------------+------------+----------+------------+
| 00001235 | 2012-09-25 | 09:11:00   | NULL       | Event 1  | NULL       |
+----------+------------+------------+------------+----------+------------+
| 00001235 | 2012-09-25 | 09:12:00   | NULL       | Event 1  | Location 1 |
+----------+------------+------------+------------+----------+------------+

I am not sure if this is possible with some set of sub selects or something, I think this is way over my league with SQL. Currently I have this working with application logic, but thought it might be cleaner and faster to handle it with SQL only.

Any help would be fantastic.

  • 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-11T21:30:20+00:00Added an answer on June 11, 2026 at 9:30 pm

    In case someone else ever runs into the issue, posting the solution here. Thanks to dianuj for steering me in the right direction for it.

    SELECT
        serial.serial as 'e_serial',
        @dt:=TIMESTAMP(history.date, history.time) as 'date_time',
        history.date,
        history.time,
        (SELECT
            employee.name
        FROM
            history
            LEFT JOIN serial ON history.serial = serial.serial
            LEFT JOIN employee ON employee.serial_id = serial.id
        WHERE
            TIMESTAMP(history.date, history.time) = (
                SELECT
                    max(TIMESTAMP(history.date, history.time))
                FROM
                    history
                WHERE TIMESTAMP(history.date, history.time) < @dt
            )
        ) as 'employee',
        event.name as 'event',
        (SELECT
            location.name
        FROM
            history
            LEFT JOIN serial ON history.serial = serial.serial
            LEFT JOIN location ON location.serial_id = serial.id
        WHERE
            TIMESTAMP(history.date, history.time) > @dt
        LIMIT 1
        ) as 'location'
    FROM history
    LEFT JOIN serial ON history.serial = serial.serial
    LEFT JOIN event ON serial.id = event.serial_id
    WHERE event.name IS NOT NULL
    ORDER BY history.date, history.time
    

    And the results

    +----------+---------------------+------------+----------+------------+---------+------------+
    | e_serial | date_time           | date       | time     | employee   | event   | location   |
    +----------+---------------------+------------+----------+------------+---------+------------+
    | 00001235 | 2012-09-25 09:01:00 | 2012-09-25 | 09:01:00 | John Smith | Event 1 | Location 1 |
    | 00001235 | 2012-09-25 09:05:00 | 2012-09-25 | 09:05:00 | NULL       | Event 1 | Location 1 |
    | 00001235 | 2012-09-25 09:10:00 | 2012-09-25 | 09:10:00 | NULL       | Event 1 | NULL       |
    | 00001235 | 2012-09-25 09:11:00 | 2012-09-25 | 09:11:00 | NULL       | Event 1 | NULL       |
    | 00001235 | 2012-09-25 09:12:00 | 2012-09-25 | 09:12:00 | NULL       | Event 1 | Location 1 |
    +----------+---------------------+------------+----------+------------+---------+------------+
    5 rows in set (0.01 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL database with a few (five to be precise) huge tables.
I have a large table I imported from an Excel spreadsheet. For five of
I have a simple mysql table with five columns: id: auto_increment (primary) id_symbol: INT(4)
I have written some SQL code to create five tables with several relations(foreign keys).
I have the following five tables: ISP Product Connection AddOn AddOn/Product (pivot table for
I have five mysql tables. shops +----+--------------+--------------+ | id | name | address |
I have five MySQL tables employee qualification institute qualification_institute (id, qualification_id, institute_id) employee_qualification_institute (employee_id,
I have the following query which groups all entries in a MySQL table by
I am stuck with a mysql query. I have three tables: Table Name :
I have five tables in my database. Members, items, comments, votes and countries. I

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.