Forum Moderators: open

Message Too Old, No Replies

Get all unique values from different tables with JOIN

         

Rando

12:10 pm on Jul 20, 2009 (gmt 0)

10+ Year Member



Hello all!

Thanks for reading at first, I've (in my opinion) a quite complex problem. I am trying to build a MySQL query which can get all unique values from one columnname from different tables. Visualised;

For example I have a shop which sells different products every month, how can I call every product?

Table January;
Hotdogs
Sausages
Cold Drinks

Table February;
Cold Drinks
Hot Drinks
Sausages

How can I query the database the way I get the following result;
Hotdogs
Sausages
Cold Drinks
Hot Drinks

Thanks for the support!

mark_roach

12:52 pm on Jul 20, 2009 (gmt 0)

10+ Year Member



Welcome to Webmasterworld.

I think the following query should the job:

select distinct product from January
union
select distinct product from February

Rando

7:49 am on Jul 23, 2009 (gmt 0)

10+ Year Member



Thanks for the fast answer!
That worked out for me, but what if I also want to include the price of both tables? Example; what if the price of the Cold Drinks was $2 in January and $3 in February, how to I get the following result?

Name Product ----- Price January --- Price February
Cold Drinks ----- $2 --- $3
etc.
etc.
etc.

Thanks in advance!

Rando