Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In mySQL, Is it possible to SELECT from two tables and merge the columns?

If I have two tables in mysql that have similar columns...

TABLEA
  id
  name
  somefield1

TABLEB 
  id
  name
  somefield1
  somefield2

How do I structure a SELECT statement so that I can SELECT from both tables simultaneously, and have the result sets merged for the columns that are the same?

So for example, I am hoping to do something like...

SELECT name, somefield1 FROM TABLEA, TABLEB WHERE name="mooseburgers";

...and have the name, and somefield1 columns from both tables merged together in the result set.

Thank-you for your help!

Sample output appended because question unclear:

I want the rows from table1 and the rows from table2 appended in the resultset. For example if the tables contain

TABLEA
id(1) name(zoot) somefield(suit)

TABLEB
id(1) name(zoot) somefield(flute)

The resultet would look like:

name     |     somefield1
zoot           suit
zoot           flute
like image 277
Travis Avatar asked May 26 '10 13:05

Travis


People also ask

How do I merge two columns in different tables in MySQL?

The USING clauseThis join combines table1 with table2 when two columns that both tables share ( id and state ) each have matching values. table1.id = table2.id AND table1. state = table2.

Can you SELECT from two tables in MySQL?

MySQL Joins let you access data from multiple tables. A MySQL Join is performed whenever two or more tables are joined in an SQL statement.

What would you use to combine columns from two different tables?

Joins are used to combine the rows from multiple tables using mutual columns.


2 Answers

You can combine columns from both tables using (id,name) as the joining criteria with:

select
    a.id                               as id,
    a.name                             as name,
    a.somefield1 || ' ' || b.somefied1 as somefield1
from tablea a, tableb b
where a.id   = b.id
  and a.name = b.name
  and b.name = 'mooseburgers';

If you want to join on just the (id) and combine the name and somefield1 columns:

select
    a.id                               as id,
    a.name || ' ' || b.name            as name,
    a.somefield1 || ' ' || b.somefied1 as somefield1
from tablea a, tableb b
where a.id   = b.id
  and b.name = 'mooseburgers';

Although I have to admit this is a rather unusual way of doing things. I assume you have your reasons however :-)

If I've misunderstood your question and you just want a more conventional union of the two tables, use something like:

select id, name, somefield1, '' as somefield2 from tablea where name = 'mooseburgers'
union all
select id, name, somefield1, somefield2 from tableb where name = 'mooseburgers'

This won't combine rows but will instead just append the rows from the two queries. Use union on its own if you want to remove duplicate rows but, if you're certain there are no duplicates or you don't want them removed, union all is often more efficient.


Based on your edit, the actual query would be:

select name, somefield1 from tablea where name = 'zoot'
union all
select name, somefield1 from tableb where name = 'zoot'

(or union if you don't want duplicates where a.name==b.name=='zoot' and a.somefield1==b.somefield1).

like image 54
paxdiablo Avatar answered Sep 28 '22 03:09

paxdiablo


I am not sure what you mean by merge, but you can UNION the results:

SELECT id, name, somefield1 FROM TABLEA WHERE name="mooseburgers"
union all
SELECT id, name, somefield1 FROM TABLEB WHERE name="mooseburgers"; 
like image 25
D'Arcy Rittich Avatar answered Sep 28 '22 02:09

D'Arcy Rittich