Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: Advanced (?) SQL help

  1. #11
    Join Date
    Dec 2006
    Location
    Uk
    Beans
    109

    Re: Advanced (?) SQL help

    Perhaps,

    Code:
    select name from photos where id in (select photo_id from photo_tags where tag_id in (6, 13);
    Last edited by winch; July 21st, 2007 at 04:36 PM. Reason: distinct not needed
    OpenStreetMap - Free editable map of the whole world

  2. #12
    Join Date
    Jun 2005
    Location
    Brazil
    Beans
    440
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Advanced (?) SQL help

    Quote Originally Posted by winch View Post
    Perhaps,

    Code:
    select name from photos where id in (select photo_id from photo_tags where tag_id in (6, 13);
    The nested SELECT will return all photos that have the tags 6 or 13, not those that have both tags.
    At the moment, I don't see any other alternative to do what the original post wants.

    Good luck!

  3. #13
    Join Date
    Oct 2006
    Beans
    513

    Re: Advanced (?) SQL help

    Yeah, I guess I'll just use it. It seems kinda strange that it's not a better way. I'd assume this kind of problem pops up fairly often

  4. #14
    Join Date
    Dec 2006
    Location
    Uk
    Beans
    109

    Re: Advanced (?) SQL help

    Quote Originally Posted by tszanon View Post
    The nested SELECT will return all photos that have the tags 6 or 13, not those that have both tags.
    Opps.

    You could also do this,

    Code:
    select name from photos where id in(select photo_id from (select photo_id, count(*) as count from photo_tags where tag_id in (6, 13) group by photo_id) where count = 2);
    That will work as long as the photo_tags table doesn't have duplicate rows.
    Last edited by winch; July 21st, 2007 at 08:34 PM.
    OpenStreetMap - Free editable map of the whole world

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •