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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:52:18+00:00 2026-05-28T02:52:18+00:00

I have a table that contains some user data: user_id | guest_id | time_seen

  • 0

I have a table that contains some user data:

user_id | guest_id | time_seen | action_performed | longitude | latitude
-------------------------------------------------------------------------  
 123     | NULL     | Jan 10    | search           | -127      | 35  
 152     | NULL     | Dec 10    | login            | -128      | 34   
 172     | NULL     | Dec 15    | search           | -125      | 35   
 123     | NULL     | Jan 10    | login            | -127      | 35   
 NULL    | GUEST1   | Jan 10    | search           | -127      | 35   
 NULL    | GUEST1   | Dec 10    | search           | -127      | 35   
 NULL    | GUEST2   | Jan 10    | browse           | -127      | 35   
 NULL    | GUEST3   | Dec 10    | browse           | -127      | 35   

I need to get a list of the unique user ids and guest ids for a given time span. The same row never has a valid user ID AND a valid guest ID at the same time. The query I’m using currently is:

SELECT * 
    FROM stats 
    WHERE time_seen >= "2011-12-1 00:00:00" 
      AND time_seen < "2012-1-1 00:00:00" 
    GROUP BY guest_id 
UNION 
SELECT * 
    FROM stats 
    WHERE time_seen >= "2011-12-1 00:00:00" 
      AND time_seen < "2012-1-1 00:00:00" 
    GROUP BY user_id;

So I’d expect to retrieve:

user_id | guest_id | time_seen | action_performed | longitude | latitude
-------------------------------------------------------------------------  
 152     | NULL     | Dec 10    | login            | -128      | 34   
 172     | NULL     | Dec 15    | search           | -125      | 35 
 NULL    | GUEST1   | Dec 10    | search           | -127      | 35
 NULL    | GUEST3   | Dec 10    | browse           | -127      | 35      

The real table contains about 11 million entries as of this time and is growing every day, so obviously I’m interested in making the query as efficient as possible. This query seems a little suboptimal (unless there’s internal optimization) since I perform the same query each time, just group it by different things afterwards.

Is there any way to improve my query, or is this the best I can do?

In response to questions below:

The dates do have a timestamp value, I just wanted to simplify for the purposes of the post.

There is no overlap between user_id values and guest_id values.

  • 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-28T02:52:18+00:00Added an answer on May 28, 2026 at 2:52 am

    If there is no overlap between the user_id values and the guest_id values then you could GROUP BY the COALESCE of those two columns and not use the UNION

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

Sidebar

Related Questions

I have a table that contains some blob fields that I don't want to
I have a table that contains intervals: CREATE TABLE tbl ( user_id: INTEGER, start:
I have a table that contains id, created, user_id. I'm wondering if there is
I have a table of data that contains about 260 rows. Each of these
I have a table that contains tasks and I want to give these an
I have a table that contains maybe 10k to 100k rows and I need
I have a table that contains members names and a field with multiple ID
I have a table that contains log entries for a program I'm writing. I'm
Here is my situation: I have one table that contains a list of drugs
This is for MySQL and PHP I have a table that contains the following

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.