Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>FROM suppliers c INNER JOIN supplier_site ss ON c.supplier_id = ss.supplier_id WHERE c.supplier_id != '0' AND ss.site_id = '2' GROUP BY c.supplier_id ORDER BY c.supplier_name ASC </code></pre> <p>Since autogenerated primary keys are never equal to 0 (unless big db design mistake) you can drop the c.supplier_id != '0' clause.</p> <p>ss.site_id = '2' should be in the JOIN condition for readability.</p> <p>It looks like this should match only one row in table supplier_site per supplier (if this is your usual 1-N thing-addresses relation, ie you're selecting the second address of each supplier, maybe '2' corresponds to 'billing address' or something) so the GROUP BY c.supplier_id is useless. If the GROUP BY actually does something, then the query is wrong, since the "address" columns, which presumably come from supplier_site table, would come from a random row.</p> <p>So here's the simplified FROM (the WHERE is gone) :</p> <pre><code>FROM suppliers c INNER JOIN supplier_site ss ON (c.supplier_id = ss.supplier_id AND ss.site_id = '2') ORDER BY c.supplier_name ASC </code></pre> <p>I suppose you got an index on c.supplier_name so this part of the query should be very fast.</p> <p>Now try this query :</p> <pre><code>SELECT a.*, questapproved, ss.supplier_no AS supplier_no, IF (active=1,'Yes', IF (active=2, 'NCR Only','Inactive')) AS rated, sum( q.incomplete = '0') AS questions, sum( q.reviewed = '1' ) AS reviewed FROM ( SELECT c.supplier_id, supplier_name, address1, address2, address3, address4, suppliertype, postcode, contact_name FROM suppliers c INNER JOIN supplier_site ss ON (c.supplier_id = ss.supplier_id AND ss.site_id = '2') ORDER BY c.supplier_name ASC LIMIT 0, 20 ) a LEFT JOIN supplier_questions q ON (q.supplier_id = c.supplier_id) GROUP BY c.supplier_id ORDER BY c.supplier_name; </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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