Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should join your table and filter the result with HAVING keyword :</p> <pre><code>SELECT games.game, COUNT(games.ga_id), ga_eq.eq_id FROM `games` JOIN ga_eq USING (ga_id) JOIN equipements USING (eq_id) GROUP BY games.ga_id HAVING COUNT(games.ga_id)=1 AND eq_id=1 </code></pre> <p>Here is the method : Get all row with </p> <pre><code>SELECT games.game, ga_eq.eq_id FROM `games` JOIN ga_eq USING (ga_id) JOIN equipements USING (eq_id) </code></pre> <p>result : <img src="https://i.stack.imgur.com/oOUoQ.png" alt="enter image description here"></p> <p>Then grouby ID and make a count a number id (so number equipement)</p> <pre><code>SELECT games.game, ga_eq.eq_id, COUNT(games.ga_id) FROM `games` JOIN ga_eq USING (ga_id) JOIN equipements USING (eq_id) GROUP BY games.ga_id </code></pre> <p><img src="https://i.stack.imgur.com/VkZWg.png" alt="enter image description here"></p> <p>Then use HAVING keyword to keep only games with one equipement and put your eq_id filter IN HAVING CLAUSE (not in WHERE clause)</p> <pre><code>SELECT games.game, ga_eq.eq_id, COUNT(games.ga_id) FROM `games` JOIN ga_eq USING (ga_id) JOIN equipements USING (eq_id) GROUP BY games.ga_id HAVING COUNT(games.ga_id)=1 AND eq_id=1 </code></pre> <p>Result : <img src="https://i.stack.imgur.com/H442f.png" alt="enter image description here"></p> <p>Change the filter <code>AND eq_id=3</code> -> empty result</p> <p>For many equipment id the query is a little bit different, I try to make one generic but didn't fint out, if someone else have an idea ?</p> <pre><code>SELECT g.game, equipements.eq_id, ( SELECT COUNT( eq_id ) FROM ga_eq WHERE ga_id = g.ga_id AND eq_id IN (1, 2) ) AS nb_eq FROM `games` g JOIN ga_eq USING ( ga_id ) JOIN equipements USING ( eq_id ) GROUP BY game HAVING nb_eq =2 </code></pre> <p>You have to change <code>AND eq_id IN (1, 2)</code> AND <code>HAVING nb_eq =2</code> to change the input</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.
 

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