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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:33:25+00:00 2026-05-20T15:33:25+00:00

Alright, I’m trying to figure out why I can’t understand how to do this

  • 0

Alright, I’m trying to figure out why I can’t understand how to do this well…

I have two tables:

  • invoices:

    • id
    • userID
    • amount
    • date
  • payments:

    • id
    • userID
    • amount
    • date

So, the goal here is to join both tables, where the userID matches whatever I want it to be – and then return everything ordered by date (most recent at the top). However, because there is a date field in each of the tables, I’m not sure how MySQL will handle things… will is sort by both dates automatically? Here’s what I was thinking…

"SELECT DISTINCT *
            FROM invoices,payments
            WHERE {$userID} = invoice.userID
            OR {$userID} = payments.userID
            ORDER BY date DESC";

But, it’s starting to become clear to me that maybe this isn’t even the right use of a join command… maybe I need to just get all data on each table alone, then try to sort it somehow with PHP? If that’s the better method, what’s a good way to do this type of DATE sort while keeping all row data in tact?

I should add, the TIME inside the unix timestamp (that’s how “date” is stored) is NOT negligible – it should sort by the date and time.

Thanks all…

  • 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-20T15:33:25+00:00Added an answer on May 20, 2026 at 3:33 pm

    If the columns of both tables are the same, you can use a UNION

    SELECT X.*
      FROM ( SELECT `id`,
                    `userID`,
                     'INVOICE' AS PTYPE
                     `amount`,
                     `date`
                FROM `invoices`
               WHERE {$userID} = userID  
              UNION
              SELECT `id`,
                     `userID`,
                     'PAYMENT' AS PTYPE
                     `amount`,
                     `date`
                FROM `payments`
               WHERE {$userID} = userID  
            ) X
     ORDER BY X.`date`
    

    EDIT

    Read the relevant section of the MySQL manual on UNIONS. There are other ways of phrasing this, but this is my preferred style – it should be clear to anybody reading that the ORDER BY clause applies to the result of both sides of the UNION. A carelessly written UNION – even with an ORDER BY – may still leave the final resultset in indeterminate order.

    The purpose of the PTYPE is that this query returns an extra column called PTYPE, that indicates whether each individual row is an INVOICE or a PAYMENT… ie. which of the two tables it comes from. It’s not mandatory, but can often be useful within a union

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

Sidebar

Related Questions

Alright I've been debugging the whole day but can't figure out what the problem
Alright I have the MYSQL query code setup to pull this information out of
Alright, this one's got me seriously stumped, after trying things out for hours with
Alright so I have no idea how to even begin doing this But basically
Alright, here I am again trying to write code from scratch and I can't
Alright, i have read many different views on how to do this with no
Alright, here is my problem i'm trying to solve. I have an index page
Alright, I have some questions... When you have a moment, can you take a
Alright. This may be difficult but I have been struggling for quite a bit
Alright, this problem seems to be way above my head! I have this code:

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.