Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

joining the output of 2 select statements

Status
Not open for further replies.

apefroggie

Technical User
Aug 19, 2003
3
US
Hello All,

If I have two select statements which both outputs a column called name and number of times the name appears and I would like to know what names appear in both results, how do I query for that?

Example - Select 1 output = {paul 3, joe 1, tommy 4}
Select 2 output = {paul 2, jan 2, tommy 1}

Output I want:
prefer: {paul 5, tommy 5}
but it is fine if I just get {paul, tommy}
 
To get the common names, I'd just use something like:

select distinct foo.name
from foo, foo2
where foo.name = foo2.name


Trying to get the aggregate counts from both tables it trickier. I think it can be done, but I'm not sure how.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Code:
select name from t1
union
select name from t2

To get the count as well, you need derived tables which is not supported in Mysql. (You can get it by populating a temp orary table with the query

Code:
select name from t1
union all
select name from t2

and then do a group by query on the temporary table.)
 
Thanks for the response.
After looking at the mysql docs I realized that what I wanted is "intersect" rather than "union", but intersect will only be implemented in future versions. In the meantime, how do I select into a new table or make a temporary table.
 
How do you populate it with something that you selected earlier
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top