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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:27:04+00:00 2026-06-15T09:27:04+00:00

I need some help figuring out the SQL for combining two tables into one

  • 0

I need some help figuring out the SQL for combining two tables into one table. I’m wanting to combine the tables and display the data on different rows for each table, but what I have below makes only one row.

Here are the tables:

CREATE TABLE tenant_invoices(
  invoice_id INT,
  col1 INT,
  col2 INT
)

CREATE TABLE tenant_payments(
  pmt_invoice_id INT,
  colA INT,
  colB INT
)

Here are the temporary tables I need to create:

CREATE TEMPORARY TABLE tenant_invoices
    SELECT * FROM invoices

CREATE TEMPORARY TABLE tenant_payments
    SELECT * FROM payments

select ti.*, tp.*
from tenant_invoices ti
left join tenant_payments tp
  on ti.invoice_id = tp.pmt_invoice_id
ORDER BY ti.invoice_date DESC;

This results in two tables merge together but only one row is created:

invoice_id   col1   col2    pmt_invoice_id   colA   ColB
1            ABC    XYZ     1                111    222

I’m wanting the table to be able to merge the tables together so I get all the columns, but instead of one row with combined data I want the rows separated so I can loop through the data in a more logical fashion:

invoice_id   col1   col2    pmt_invoice_id   colA   ColB
1            ABC    XYZ     null             null   null
                            1                111    222 

Is there a way I can accomplish this with SQL?

Thanks.

  • 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-15T09:27:05+00:00Added an answer on June 15, 2026 at 9:27 am

    To achieve this result…

    invoice_id   col1   col2    pmt_invoice_id   colA   ColB
    1            ABC    XYZ     null             null   null
                                1                111    222 

    You can use UNION ALL like so:

    SELECT invoice_id, col1, col2, NULL AS pmt_invoice_id, NULL AS colA, NULL AS col
    FROM tenant_invoices
    UNION ALL
    SELECT NULL, NULL, NULL, pmt_invoice_id, colA, colB
    FROM tenant_payments
    

    As already mentioned, to simply stack the two tables on top of each other, then you align the columns and use UNION ALL. If you wish to remove duplicates, you can use UNION.

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

Sidebar

Related Questions

I need some help figuring out a query I have 3 tables sources id,
I need some help figuring out and SQL Statement. I know what I want
I need some help figuring out how I should do this. I would like
I need some help figuring out how to do something. I got this gallery
Hey all, i am in need of some help with figuring out how to
I'm a n00b and I need some help figuring out this issue, I would
I need some help figuring out how to write this .htaccess . My file
I need some help figuring out what I'm doing wrong here. I am trying
need some help figuring this out. My function: function cleanDatabase() { $allowedTime = $this->time
I need some help with figuring out Pythons *args and **kwargs . It's simple

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.