Thanks dragonchild, i did mean to mention that i had the sql for the first link and not the others.

and Thanks simonm thats exactly what I had in mind but I had no clue that you could join a table more than once. Off i go looking thre MySQL documentation with a new point of view. How many joins do you do in your case? How many is too many? Just ideas, I know that it will vary from case to case, and depend on the size of the table etc.

Here is the code i came up with so far. Its a bit rough, but i thought others might find it useful!

use Data::Dumper; print GetNodes({name => "hello",type => "god", color => "greeen"}); sub GetNodes { my $meta = shift; #expects a hash of {name => value} print Dumper($meta); my $sql = "SELECT md1.node_id FROM "; my @From; # list of tables my @Where; # list of conditions my @LastWhere; my $i = 0; foreach my $name (keys %$meta) { $i ++; push @From, "`meta_data` as md$i"; push @Where,"md$i.name = \"" . $name . "\""; push @Where,"md$i.value = \"" . $meta->{$name} . "\""; push @LastWhere,"md$i.node_id"; } $sql .= join(",",@From) . " WHERE "; $sql .= join(" AND ", @Where); $sql .= " AND " .join(" = ",@LastWhere) . ";"; return $sql; }
outputs
SELECT md1.node_id FROM `meta_data` as md1,`meta_data` as md2,`meta_data` as md3 WHERE md1.name = "type" AND md1.value = "god" AND md2.name = "color" AND md2.value = "greeen" AND md3.name = "name" AND md3.value = "hello" AND md1.node_id = md2.node_id = md3.node_id;
Thanks!
Update: Added above code :-)
Eric Hodges

In reply to Re: Re: Re: Querying Meta Data by eric256
in thread Querying Meta Data by eric256

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.