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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:52:18+00:00 2026-06-14T19:52:18+00:00

What is the mysql I need to achieve the result below given these 2

  • 0

What is the mysql I need to achieve the result below given these 2 tables:

table1:

+----+-------+
| id | name  |
+----+-------+
|  1 | alan  |
|  2 | bob   |
|  3 | dave  |
+----+-------+

table2:

+----+---------+
| id | state   |
+----+---------+
|  2 | MI      |
|  3 | WV      |
|  4 | FL      |
+----+---------+

I want to create a temporary view that looks like this

desired result:

+----+---------+---------+
| id | name    | state   |
+----+---------+---------+
|  1 | alan    |         |
|  2 | bob     | MI      |
|  3 | dave    | WV      |
|  4 |         | FL      |
+----+---------+---------+

I tried a mysql union but the following result is not what I want.

create view table3 as
(select id,name,"" as state from table1)
union
(select id,"" as name,state from table2)

table3 union result:

+----+---------+---------+
| id | name    | state   |
+----+---------+---------+
|  1 | alan    |         |
|  2 | bob     |         |
|  3 | dave    |         |
|  2 |         | MI      |
|  3 |         | WV      |
|  4 |         | FL      |
+----+---------+---------+
  • 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-14T19:52:19+00:00Added an answer on June 14, 2026 at 7:52 pm

    You need to use an SQL operator called JOIN to get what you want.

    JOIN is a fundamental part of the SQL language, as much as a while loop is for other programming languages.

    Start here: A Visual Explanation of SQL Joins

    Another way of thinking of JOIN vs. UNION is:

    • UNION appends rows together.
    • JOIN appends columns together.

    For example:

    SELECT * 
    FROM table1
    JOIN table2 USING (id);
    

    This is a style of join that returns only the rows for which a matching id exists in both tables. But you want all of those id’s for which a row exists in at least one table or the other.

    SELECT * 
    FROM table1
    LEFT OUTER JOIN table2 USING (id);
    

    This gets all the rows from table1, with their matching rows in table2. But to get the reverse, we’d need:

    SELECT * 
    FROM table1
    RIGHT OUTER JOIN table2 USING (id);
    

    SQL also defines a FULL OUTER JOIN which would do both at once, but unfortunately MySQL hasn’t implemented that part of the SQL standard. But we can use UNION to combine the two types of joins. Union will eliminate duplicate rows by default.

    update: I troubleshot this, and got it to work. It turns out you have to name the columns explicitly, to make sure they are in the same columns in both of the unioned queries. Here’s the query that works, copied from my terminal window:

    SELECT id, name, state
    FROM table1
    LEFT OUTER JOIN table2 USING (id)
    UNION
    SELECT id, name, state
    FROM table1
    RIGHT OUTER JOIN table2 USING (id);
    

    It’s a good habit in general to avoid using SELECT *, and this is just one more case where it’s a good idea.

    Also, I had left a superfluous semicolon in the query example before. That was just a typo on my part.

    This query produces the following output, copied from my terminal window:

    +----+------+-------+
    | id | name | state |
    +----+------+-------+
    |  1 | alan | NULL  |
    |  2 | bob  | MI    |
    |  3 | dave | WV    |
    |  4 | NULL | FL    |
    +----+------+-------+
    

    PS: Thank you for asking the question so clearly!

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

Sidebar

Related Questions

I am newbee with PHP and MySQL and need help... I have two tables
I have a MySQL database and I need to turn its tables into xml
I have two tables on MySQL, with this structure: Users: id | name Accounts:
I have Table1 and I need to get it to look like Table2: Table1
I need to used dynamic order query in mysql and i have successfully achieved
Mysql: i need to get the offset of a item in a query. I
Why you may ask? Because i have built the app on mysql and need
I need a mysql query to extract two users who who live on same
I need an MySQL Skript which does the following: delete chunks of the database
I need a mysql query which will select all rows that contain joe in

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.