0

I have two tables with completely different structures. I want to query MYSQL to get all columns from both tables in a single query.

So for example, if I have a Person table and an Animal table, I want my query to return:

person_name|person_age|animal_type|animal_name
______________________________________________
Ryan       |31        |NULL        |NULL
Fred       |23        |NULL        |NULL
NULL       |NULL      |Cat         |Whiskers
NULL       |NULL      |Dog         |Wishbone

I only have basic knowledge of MYSQL and I couldn't understand most of the material I was reading and googling, and it didn't seem to match exactly what I want.

Thanks!

Nick Manning
  • 2,828
  • 1
  • 29
  • 50

1 Answers1

3

You want a UNION ALL query that concatenates two result sets

SELECT person_name, person_age,
       NULL animal_type, NULL animal_name
  FROM person
 UNION ALL 
SELECT NULL person_name, NULL person_age,
       animal_type, animal_name
  FROM animal

Each SELECT query shapes its result set in a similar way, and then the UNION ALL puts them together.

O. Jones
  • 103,626
  • 17
  • 118
  • 172