mysql - How to implement keyword search? -


i have item database want able search pre-defined keywords.

what have right 3 tables this:

items_tbl:

item_idx, item (unique), description, etc.

keyword_tbl:

kw_idx, kw (unique), kw_description

cross_tbl:

item_idx, kw_idx.

i want "and"-search... like... give me (distinct) every item have keywords "aquamarine", "blue", , "green". i.e. "aquamarine" & "blue" & "green"...

searching "or"-search quite simple, stumped doing "and" search this... in ex. want find items both "blue" , "green", not items "green".

select   items_tbl.*     items_tbl     join cross_tbl   using (item_idx)     join keyword_tbl using (kw_idx)    kw_description in ('aquamarine','blue','green') group item_idx having   count(*) = 3 

this assumes keywords can associated items @ 1 time (i.e. unique constraint on (item_idx, kw_idx) in cross_tbl); if not case, have replace count(*) less efficient count(distinct kw_idx), or less efficient count(distinct kw_description) if same keyword can appear multiple times in keyword_tbl (i.e. no unique constraint on kw_description column).


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -