working in rails app, have following table structure (pertinent columns only)
photos (id: integer) taggings (photo_id: integer, tag_id: integer) tags (id: integer, name:string)
i have following sql query:
select distinct photos.* \"photos\" inner join \"taggings\" on \"photos\".\"id\" = \"taggings\".\"photo_id\" inner join \"tags\" on \"tags\".\"id\" = \"taggings\".\"tag_id\" \"tags\".\"name\" in ('foo', 'bar')
when generate query i'm passing in array of tags (in case ["foo","bar"]
). query correctly searches photos match of tags passed in array.
how can change query select records of given tags (ie photo matches if tagged "foo" , "bar", instead of selecting records of given tags?
there may better way, should it
select photos.id,max(othercolumn) \"photos\" inner join \"taggings\" on \"photos\".\"id\" = \"taggings\".\"photo_id\" inner join \"tags\" on \"tags\".\"id\" = \"taggings\".\"tag_id\" \"tags\".\"name\" in ('foo', 'bar') group photos.id having count(*) = 2 --2 number of items in array of tags.
Comments
Post a Comment