Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>SELECT pc.* FROM products_categories pc INNER JOIN products_categories_relations pcr ON (pc.category_id = pcr.category_id) INNER JOIN products p ON (pcr.product_id = p.product_id AND published = 0) EXCEPT SELECT pc.* FROM products_categories pc INNER JOIN products_categories_relations pcr ON (pc.category_id = pcr.category_id) INNER JOIN products p ON (pcr.product_id = p.product_id AND published != 0) </code></pre> <p>Essentially you query for the categories that have products with published = 0 and remove from the set all that have products with published other than 0. It will result in exactly what you need.</p> <p>Note that category with products with published = 0 is not the same as empty category. If you need empty categories as well, just make a <code>union all</code>.</p> <p>EDIT: As I was just reminded that MySQL <strong>still</strong> don't have some of the basic SQL operations here is work around:</p> <pre><code>SELECT pc.* FROM products_categories pc INNER JOIN products_categories_relations pcr ON (pc.category_id = pcr.category_id) INNER JOIN products p ON (pcr.product_id = p.product_id AND published = 0) WHERE pc.product_category_id NOT IN ( SELECT distinct pc.product_category_id FROM products_categories pc INNER JOIN products_categories_relations pcr ON (pc.category_id = pcr.category_id) INNER JOIN products p ON (pcr.product_id = p.product_id AND published != 0) ) </code></pre> <p>or you can also make it:</p> <pre><code>SELECT * FROM ( SELECT distinct pc.* FROM products_categories pc INNER JOIN products_categories_relations pcr ON (pc.category_id = pcr.category_id) INNER JOIN products p ON (pcr.product_id = p.product_id AND published = 0) ) subQ1 LEFT OUTER JOIN ( SELECT distinct pc.* FROM products_categories pc INNER JOIN products_categories_relations pcr ON (pc.category_id = pcr.category_id) INNER JOIN products p ON (pcr.product_id = p.product_id AND published != 0) ) subQ2 ON subQ1.product_category_id = subQ2.product_category_id WHERE subQ2.product_category_id IS NULL </code></pre> <p>I suspect the first should be faster but you can give a try to both.</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. 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