I hesitated posting this question, because it actually seems to be two poor questions. First, is there any way to use a combined query (PostgreSQL docs) through DBIx::Class? I've looked through SQL::Abstract and the DBIx cookbook to no avail. At first attempt, I can't even use search_literal because the string is stuffed into a WHERE clause:

$schema->resultset('foo')->search_literal('EXCEPT SELECT * FROM bar')
becomes something like
SELECT * FROM foo WHERE ( EXCEPT SELECT * FROM BAR )
which obviously isn't right (or even valid SQL).

The alternate/base question is really more SQL than Perl, but I'll go ahead and describe it anyway; I'm sure someone here has done something like this before.

Suppose I have two tables (say containers and items, with just ID columns) with a many-to-many mapping table (mapping, with two columns: container_id and item_id). Any given container can hold zero or more items, and any given item can be contained in zero or more containers. Finding out what items are in a given container (say '1') is easy:

$schema->resultset('items')->search({'mapping.container_id' => 1}, {jo +in => 'mapping'});
or the roughly equivalent SQL
SELECT * FROM items LEFT JOIN mapping ON (items.item_id = mapping.item +_id) WHERE mapping.container_id = 1

However, the "inverse" query is not as obvious. How do I get a list of all items that are not in a given container? Simply modifying the above to say "container id != 1" doesn't work since an item could be in both container 1 and container 2. The following SQL will do it:

SELECT items.item_id FROM items LEFT JOIN mapping ON (items.item_id = +mapping.item_id) EXCEPT SELECT items.item_id FROM items LEFT JOIN mapping ON (items.i +tem_id = mapping.item_id) WHERE mapping.container_id = 1
but that feels wasteful, with the added complication of translating it into DBIx::Class. Anyone have any suggestions? (Obviously the above tables are somewhat contrived; I tried to give enough information, but let me know if I need to elaborate some point.)


In reply to Combined SQL Queries (e.g., EXCEPT) and DBIx::Class by jgallagher

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.