Thanks! Adding a table does the trick at the cost of some reworking of adds and fetches. Is this the SQL equivalent of "Add another level of indirection"?

Reworked sample is:

use strict; use warnings; use DBI; my $dbh = DBI->connect('dbi:SQLite:dbname=xr.SQLite'); $dbh->do(<<SQL); CREATE TABLE Sessions ( SessId INTEGER PRIMARY KEY AUTOINCREMENT, DoneDate TEXT, Topic TEXT ) SQL $dbh->do(<<SQL); CREATE TABLE SessPeople ( SessId INTEGER, PersonId INTEGER, IsTeacher INTEGER ) SQL my $self = bless {dbh => $dbh}; $self->addSession(1, 2, 'Perl is fun', '2015-06-27') +; $self->addSession(3, 4, 'SQL is fun', '2015-06-27') +; $self->addSession(3, 5, 'SQL for fun and profit', '2015-06-20') +; $self->addSession(4, 1, 'PerlMonks is great', '2015-06-20') +; $self->addSession(2, 6, 'PerlMonks for fun and proffit', '2015-06-20') +; my $active = $self->getActiveSessions(1, 2); print "$_->[1], $_->[2]: '$_->[3]'\n" for @$active; sub addSession { my ($self, $teacher, $learner, $topic) = @_; $self->{dbh}->do(<<SQL, {}, $topic); INSERT INTO Sessions (Topic, DoneDate) VALUES (?, '') SQL my $sessId = $self->{dbh}->last_insert_id(undef, undef, undef, und +ef); $self->{dbh}->do(<<SQL, {}, $sessId, $teacher); INSERT INTO SessPeople (SessId, PersonId, IsTeacher) VALUES (?, ?, 1) SQL $self->{dbh}->do(<<SQL, {}, $sessId, $learner); INSERT INTO SessPeople (SessId, PersonId, IsTeacher) VALUES (?, ?, 0) SQL } sub getActiveSessions { my ($self, @ids) = @_; my $idPlaces = (join ', ', ('?') x @ids) || ''; return $self->{dbh}->selectall_arrayref(<<SQL, {}, @ids); SELECT DISTINCT s.SessId, t.PersonId, l.PersonId, s.Topic FROM Session +s s JOIN SessPeople t ON t.SessId == s.SessId AND t.IsTeacher JOIN SessPeople l ON l.SessId == s.SessId AND NOT l.IsTeacher JOIN SessPeople p ON p.SessId == s.SessId WHERE p.PersonId IN ($idPlaces) SQL }
Perl is the programming world's equivalent of English

In reply to Re^2: Avoiding SQL double jeopardy by GrandFather
in thread Avoiding SQL double jeopardy by GrandFather

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.