php - MYSQL get closest match with multiple WHERE -


in database, have table products , table has tags associated each of te products.

when user looks @ product's page, want show "related" (or closest) product 1 he's looking at.

so let's product tagged 5 different tags. prodcuts have same 5 tags , ones have 4 of same 5 tags, , ones have 3 of same 5 tags, etc...

to this, i'm guessing have create 1 or more mysql query don't know start.

matching same 5 tags easy, can use tag='?' , tag='?'... how can other (4/5, 3/5, 2/5, 1/5)?

any appreciated!

cheers steven


edits

@orbits: tags on different rows... if not text match the case. tag row consists of (id, tag, product_id)

@cusimar9: different table, stated in post :p

@vbence: believe it's simple possible.. here is... don't have connecting table

products :

create table `products` (   `id` int(11) unsigned not null auto_increment,   ...   primary key (`id`) ) engine=myisam  default charset=utf8; 

tags :

create table `tags` (   `id` int(11) not null auto_increment,   `type` varchar(70) not null,   `product_id` int(11) not null,   primary key (`id`) ) engine=myisam  default charset=latin1 auto_increment=116 ; 

the string literals 'tag1' , has replaced tags of course.

select products.id, count(tags.id) match_level products     left join tags on tags.product_id = product.id tags.type in ('tag1', 'tag2', 'tag3', 'tag4', 'tag5') group products.id order match_level desc 

if don't have index already, add tags table. without indexes wasting resources. big time.

create index multimatch on tags (type, product_id); 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -