Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can select the individual Ids who answered one of the questions, like this:</p> <pre><code>select distinct memberId where answerId = 'dogs' select distinct memberId where answerId = 'red' </code></pre> <p>Then replace the ands with inner joins, like this:</p> <pre><code>select memberId from (select distinct memberId where answerId = 'dogs') D inner join (select distinct memberId where answerId = 'red') R on D.MemberId = R.memberId </code></pre> <p>And replace the OR with UNION, like this:</p> <pre><code>select memberId from (select distinct memberId where answerId = 'dogs') D inner join (select distinct memberId where answerId = 'red') R on D.MemberId = R.memberId union select memberId from (select distinct memberId where answerId = 'cats') D inner join (select distinct memberId where answerId = 'green') R on D.MemberId = R.memberId </code></pre> <p>This is exactly, as you say, a "set sintax":</p> <pre><code>Set: each indivdual select Intersection of sets : inner join Union of sets: union </code></pre> <p>If you need extra conditions, for example checking only answers 1 and 2, you can use a CTE with the common conditions like "answerId in (1,2)" to avoid writing this same condition in every select, making the sintax more clear.</p> <p>You can also use this sintax, which is more similar to your original writing:</p> <pre><code>select memberId from members M where ( exists (select 1 from answers where answerId='dogs' and memberId=M.member Id) and exists (select 1 from answers where answerId='red' and memberId=M.member Id) ) or ( exists (select 1 from answers where answerId='cats' and memberId=M.member Id) and exists (select 1 from answers where answerId='green' and memberId=M.member Id) ) -- I select 1 instead of * or any column for performance reasons </code></pre> <p>I leave out the details of adding extra checks (answerId and so on) to make it easier to understand.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload