in reply to SQL query: are all results in one hashref key the same?

This is not a Perl question, and you didn't even show us what your tables look like. However, I'm betting you can find the answer in this SQL tutorial where it discusses GROUP BY and HAVING clauses.
  • Comment on Re: SQL query: are all results in one hashref key the same?

Replies are listed 'Best First'.
Re: Re: SQL query: are all results in one hashref key the same?
by Cody Pendant (Prior) on Oct 11, 2003 at 04:07 UTC
    Yes, sorry I didn't say "I know this isn't strictly Perl" -- I usually do.

    To set the record straight, I am doing it in Perl, and for more detail of what I'm doing with DBI -- what the tables look like is more or less irrelevant, but essentially my select is:

    select actor_name, episode_title, character_name FROM <my tables> WHERE <certain table columns match> AND actor_id = $id

    And this results in rows representing a list of episodes an actor has appeared in. Most of the time he or she played the same character, so the character_name is the same in every row.

    I wanted to be able to do a preliminary type of select which returned either one or more-than-one rows depending on whether that actor played one or more roles in total.

    It does seem that GROUP BY will do what I want. Thanks.



    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
      Slightly off topic... but since you mentioned it: PLEASE don't actually put literal values into your sql by string interpolation: $sql = "... thing_id = $thing_id ...";. Do it with placeholders and bind values. This can be more and less of a performance issue with different types of databases, but it is always a security issue. In some databases, I could specify a $thing_id of "5; drop table thing", and you'd be hating life. On databases where that sort of thing can't be made to work (like oracle, for example), I could still plant a denial of service attack by saying that $thing_id was
      "(select min(thing1.thing_id) from thing thing1, thing thing2, thing thing3, thing thing4, thing thing5, thing thing6, thing thing7, thing thing8, thing thing9, thing thing10 --look! no 'where', clause this is a 10-way cartesian product of thin +g! )"
      You can come back in a month when your database finishes processing that query.

      ------------
      :Wq
      Not an editor command: Wq
        Thanks for reminding me. I didn't understand the second example, but I sure as hell understood the first. I will certainly make sure that stuff is sorted out before the site becomes public.


        ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
Re: Re: SQL query: are all results in one hashref key the same?
by Anonymous Monk on Oct 11, 2003 at 21:54 UTC
    I just wish I could lay my hands on the book where this was given as an example! Maybe if Cody read that he would find the answer??