I’m still a SQL-rookie. I have three tables with a given structure:
table1 (products)
--------------------------------------
id ident title types desc
--------------------------------------
01 111ab title1 2 details 1
02 222ab title2 1 details 2
03 333ab title3 2 details 3
A product can belong to many product-types. The “types”-column is the number of types related to a product. Product “111ab” belongs to 2 product-types.
table2 (product-types)
--------------------------
id type typedesc
--------------------------
01 type1 description 1
02 type2 description 2
03 type3 description 3
table3 (relations)
-------------------
id tbl1id tbl2id
-------------------
01 01 01
02 02 03
03 03 03
04 01 03
05 03 02
Table3 shows the relations between table1 (products) and table2 (product-types). Product “111ab” is related to the product-types “01” and “03”.
I want to create a data-structure which should be something like this:
type-description prod title desc
-------------------------------------------
type1-description1 111ab title1 details1
type2-description2 333ab title3 details3
type3-description3 111ab title1 details1
type3-description3 222ab title2 details2
type3-description3 333ab title3 details3
This structure will end up in an assoc-array of product-types with the related products as assoc-sub-arrays, sorted by “ident” of table1:
type1 description1
111ab title1 details1
type2 description2
333ab title3 details3
type3 description3
111ab title1 details1
222ab title2 details2
333ab title3 details3
This array will be json-encoded and sent as a query-result of an ajax-request.
I read the threads about JOINing tables here at stackoverflow but can not generate a working SELECT-structure. Maybe this can not be done by a single query. I don’t know.
Please, can anyone give me a little help?
an
INNER JOINwill suffice your requirements,or