mysql select query on two tables

Sclerato picture Sclerato · Jan 24, 2013 · Viewed 11.3k times · Source

I am trying to get a result from two tables without having a second query nested inside the first query loop.

I have a table products:

product_code_1234 | product_name | otherfields...

And a table categories, where a product can have multiple categories:

category_name_1 | product_code_1234
category_name_2 | product_code_1234
category_name_3 | product_code_1234

Is there a query to get the following result?

product_code_1234 | product_name | ... | category_name_1 | category_name_2 | category_name_3

Answer

Edward Wong picture Edward Wong · Jan 24, 2013
select * from a,b 

will give you all data from table a ,combined with all data from table b.

However if you don't want repetition of data and there isn't any connection between table a and b it can't be done without some union or similar