Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>Select ... From Table_B As B Left Join ( Select AB.id_B, A.id, A.status From Table_A_has_B As AB Join Table_A As A On A.id = AB.id_A Where A.status = 1 ) As Z On Z.id_B = B.id </code></pre> <p>This portion of the original post is not clear: <code>if a Table_B record hasn't any associated Table_A also should be selected.</code>. </p> <p>If you only want rows from Table_B then:</p> <pre><code>Select B.* From Table_B As B Where Exists ( Select 1 From Table_A_has_B As AB Join Table_A As A On A.id = AB.id_A Where A.status = 1 And AB.id_B = B.id ) </code></pre> <p>If you want rows from <code>Table_ B</code> where either there are no rows in <code>Table_A_has_B</code> for a given <code>id_B</code> or where if there are rows it must be associated with a Table_A item where <code>status</code> is 1, then :</p> <pre><code>Select B.* From Table_B As B Where Exists ( Select 1 From Table_A_has_B As AB Join Table_A As A On A.id = AB.id_A Where A.status = 1 And AB.id_B = B.id ) Or Not Exists ( Select 1 From Table_A_has_B As AB Where AB.id_B = B.id ) </code></pre> <p>If what you want is rows from Table_A but only where the status is 1 and for all others a null, then the first query I provided would be the solution. Providing us with some expected output results would obviously help tremendously.</p> <p><strong>Edit given update to OP</strong></p> <p>Given your update, which only now makes it clear what you are actually trying to achieve, you do the following:</p> <pre><code>Select B.id From Table_B As B Where Not Exists ( Select 1 From Table_A_has_B As AB Join Table_A As A On A.id = AB.id_A Where A.status &lt;&gt; 1 And AB.id_B = B.id ) </code></pre>
    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.
    3. 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