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

    1. include tag -> search product these tags

    2. 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

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -