mysql - IN and NOT IN clause do not apply -


so have query:

select m.id, (   select group_concat(     distinct tag_id     order tag_id     separator '-'   ) tagging   mng_id = m.id   order tag_id desc ) tags  product m      inner join tagging tg on (m.id = tg.mng_id) 1   , tg.tag_id in (34,20) , tg.tag_id not in (42)   , (nme 'tomo%' or alt_nme 'tomo%') group m.id 

this query should return records tag # 34, 20 , not have tag # 42, record's name must start 'tomo'.

but reason, doesn't remove product tag #42 results. can 1 identify issue query?

if want records tags 34 , 20, not 42, try this:

select p.id product p inner join      tagging tg      on p.id = tg.mng_id group p.id having sum(tg.tag_id = 34) > 0 ,        sum(tg.tag_id = 20) > 0 ,        sum(tg.tag_id = 42) = 0; 

if want resulting tags, add group_concat(tg.tag_id).

this seems simpler approach.


Comments