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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:39:51+00:00 2026-05-24T20:39:51+00:00

I’m trying to SELECT from two tables and ORDER BY date (a column they

  • 0

I’m trying to SELECT from two tables and ORDER BY date (a column they both have). One table (tableA) has a column called “A” and the other table (tableB) has a column called “B”, I use array_key_exists() to differentiate between the two (If “A” key exists, I run the array through FunctionA(), if “B” key exists, I run the array through FunctionB()). I only need the 20 latest (date wise) entries. I need the SQL Query to accomplish this.

I already know a reply will be “if they’re similarly structured, then you should just use a single table”, but I don’t want to do that because tableA is drastically different from tableB (a lot more columns in tableA), and using a single table to store the data would result in a LOT of empty columns for entries formatted for tableB, not to mention it’d be a very ugly looking table format due to tableB not needing the majority of tableA’s columns).

I just want to display data from both tables in an ordered (by date) fashion, and in one single stream.

I need to SELECT WHERE tableA.poster_id = $id and tableB.receiver_id = $id by the way.

SOLUTION:

I’m updating this just in case anyone else with the same dilemma comes along. After implementing the SQL query that @Erik A. Brandstadmoen had graciously given me, this is basically what my code ended up as:

$MySQL->SQL("SELECT * FROM
              (SELECT A.id AS id, A.date AS date, 'tableA' AS source 
                 FROM tableA A WHERE A.poster_id = $id 
                    UNION
                      SELECT B.id AS id, B.date AS date, 'tableB' AS source
                           FROM tableB B WHERE B.receiver_id = $id) AS T
                               ORDER BY T.date DESC LIMIT 0, 20");

$GetStream = array();
$i = 0;
while ($row = mysql_fetch_array($MySQL->Result))
{
    $GetStream[$i]['id'] = $row['id'];
    $GetStream[$i]['date']=$row['date'];
    $GetStream[$i]['source'] = $row['source'];
    $i++;
}

*** later on down the code ***

$i = 0;
while ($i<count($GetStream))
{
    if ($GetStream[$i]['source'] == "tableA")
    { 
        FunctionA($GetStream[$i]);
    }
    else
    {
        FunctionB($GetStream[$i]);
    }
    $i++;
}
  • 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-24T20:39:51+00:00Added an answer on May 24, 2026 at 8:39 pm

    Try using UNION:

    SELECT * FROM (
    SELECT A.col1 AS x, A.col2 As y, A.col3 AS date FROM tableA A
    WHERE tableA.poster_id = $id
    UNION 
    SELECT B.colA AS x, B.colB AS y, B.colC AS date FROM  tableB B
    WHERE tableB.receiver_id = $id
    )
    ORDER BY date DESC
    LIMIT 0, 20
    

    OR, IF you would like to keep duplicates between tableA and tableB, use UNION ALL instead.

    EDIT, according to your comments, I understand that you need a column indicating which table the row is from. You can just add a static column in the select, like this:

    SELECT * FROM (
    SELECT A.col1 AS x, A.col2 As y, A.col3 AS date, 'A' as source FROM tableA A
    WHERE tableA.poster_id = $id
    UNION 
    SELECT B.colA AS x, B.colB AS y, B.colC AS date, 'B' as source FROM  tableB B
    WHERE tableB.receiver_id = $id
    )
    ORDER BY date DESC
    LIMIT 0, 20
    

    This gives you a nice table on the following form:

    x    y    date    source
    =========================
    (v1) (v2) (d1)    'A'
    (v3) (v4) (d2)    'B'
    (v1) (v2) (d3)    'B'
    (v3) (v4) (d4)    'A'
    

    That does what you want, doesn’t it? It’s a bit difficult understanding what you are really trying to achieve with this…

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.