开发者

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
  s开发者_如何学JAVAomefield1

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


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).


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"; 


Depending on what you mean by merge, here's a possible solution.

Select id,name,somefield1 from TableA
WHERE Name ='mooseburgers'
UNION
Select id,name,somefield1 from TableB
WHERE Name ='mooseburgers'

Will allow you to show results from both tables with the results merged into 1 table.


Do you perhaps mean

SELECT tableA.id, tableA.name, tableA.somefield1
FROM tableA, tableB
WHERE tableA.name = tableB.name AND tableA.name="mooseburgers"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜