mysql - Related products query? -
i'm trying related products database. in example below trying related products product id of 1. query should select information product 1, (it's type, colour, size , style) , select other products (not including itself) has either same type, colour size or style.
select * prds t1, prds t2 t1.id = 1 , t2.id != 1 , (t1.type_id = t2.type_id or t1.colour_id = t2.colour_id or t1.size_id = t2.size_id or t1.style_id = t2.style_id) , t2.published = 1
the above query returns product 1 number of times. going wrong?
i prefer write cross join
(because abhor commas in from
clause). but, problem select *
. instead:
select t2.* prds t1 cross join prds t2 t1.id = 1 , t2.id <> 1 , (t1.type_id = t2.type_id or t1.colour_id = t2.colour_id or t1.size_id = t2.size_id or t1.style_id = t2.style_id ) , t2.published = 1;
the real difference from
clause. version selecting columns both tables. not have same column names, first columns product 1 record.
Comments
Post a Comment