php - Search for records with similar tag id -
i have tagging table looks this:
id | product_id | tag_id --------------------------- 1 | 1 | 10 2 | 1 | 12 3 | 2 | 10 4 | 3 | 11
i'm creating search page take tags input user (1 or more) , search database records tags present.
example:- if user entered '10' table above result product_id 1 & 2.
edit:
there 2 user input
include tag -> search product these tags
exclude tag -> search products without these tags
so search query have first ignore exclude tags search product 'include tag'.
edit 2:
the user can enter 1 or more tag.
any idea how this?
here simple approach using aggregation , having
:
select tt.product_id tagtable tt group tt.product_id having sum(tt.tag_id in ($excludedtags)) = 0 , sum(tt.tag_id in ($includedtags)) = #numberincludedtags;
you need put in number of included tags second condition. also, assumes tags table not have duplicated rows (if so, fixed using count(distinct)
, clutters query).
Comments
Post a Comment