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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:25:04+00:00 2026-05-22T16:25:04+00:00

I know I can do joins but its not exactly what I want. I’m

  • 0

I know I can do joins but its not exactly what I want.

I’m making a live chat system that has 2 tables mainly: the main chat table (call it table a), and then a mod table (call this one table b). If a user gets suspended, messages reach over 100 for that channel, or they are over 1 week, the messages get moved from the main chat table to the mod table.

I store the ID of the chat messages as ID(primary) on the main chat table and as chatID on the mod table.

What I’m doing is making a separate page for my Mods and I want to be able to combine the two tables into 1 area but I want them to be ordered by their respective tables.

So lets say we had the following:

Main table ID’s: 1,2,4
Mod table ID: 3

I want my results to show up 1,2,3,4 no matter which table the ID is in.

Any help is greatly appreciated!

Edit: I got the answer and this is what I used to do so:

SELECT ab.* FROM 
  ((SELECT ID as table_id FROM a 
    WHERE roomID = 'newUsers' ORDER BY ID ASC) 
UNION ALL 
  (SELECT chatID as table_id FROM b 
    WHERE roomID = 'newUsers' ORDER BY chatID ASC)) ab 
ORDER BY ab.table_id 
  • 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-22T16:25:05+00:00Added an answer on May 22, 2026 at 4:25 pm

    Use a UNION in a subselect.

    SELECT ab.* FROM (
      SELECT 1 as table_id, * FROM a
    UNION ALL
      SELECT 2 as table_id, * FROM b
    ) ab
    ORDER BY ab.id
    

    If you want the result of table A to appear before table B, change the query to:

    SELECT ab.* FROM (
      SELECT 1 as table_id, * FROM a
    UNION ALL
      SELECT 2 as table_id, * FROM b
    ) ab
    ORDER BY ab.table_id, ab.id
    

    Some background
    UNION ALL will merge two tables resultsets into one resultset.
    UNION will do the same but will eliminate duplicate rows.
    This takes time and slows things down, so if you know there will be no duplicate records (or you don’t care about dups) use UNION ALL.

    See also: http://dev.mysql.com/doc/refman/5.5/en/union.html

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

Sidebar

Related Questions

I know that can't use left join in indexed view. but I don't understand
I have a query that I know can be done using a subselect, but
I know you can not set a key value dynamically, but what about the
I want to know can we have a JPanel with a Layout other than
Sometimes, I'll end up having to catch an exception that I know can never
You can know if the event stack is empty calling the gtk.events_pending() method, but
I know this question comes up often, but today I can't find the answer
Here is my php code. I want to know if its possible to make
I know the question has been posted before, not answered thoroughly though. Also I
I have a table that has a bunch of fields. The fields can be

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.