mysql - SQL JOIN: Select Records from Another Table With Matching IDs -


i having trouble building correct sql join statement select records table.

--table product: id name catid1 catid2 

and

--table category: catid categoryname 

product.catid1, product.catid2 referenced category.catid

so want select product fields , replace product.catid1, product.catid2 category.categoryname (for product.catid1) , category.categoryname (for product.catid2).

this not work explains need:

select product.id, product.name,         category.categoryname product.catid1,         category.categoryname product.catid2  product, categories; 

all need double left join categories table:

select p.id, p.name,         c1.categoryname catid1,         c2.categoryname catid2  product p  left join categories c1 on p.catid1 = c1.catid left join categories c2 on p.catid2 = c2.catid 

if there no match either catid1 or catid2, corresponding field in select clause going null.


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

json - Gson().fromJson(jsonResult, Myobject.class) return values in 0's -