Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Change your query to this:</p> <pre><code> SELECT u.user_id, us.orig_posts_count, us.sugg_posts_count, IF(us.user_id='4', 'posterview', COUNT(CASE WHEN p.sugg_by='2' AND (p.private='0' OR pv.post_id IS NOT NULL) THEN 1 ELSE NULL END)) AS display_orig_posts_count, IF(us.user_id='4', 'posterview', COUNT(CASE WHEN p.sugg_by&lt;&gt;'2' AND (p.private='0' OR pv.post_id IS NOT NULL) THEN 1 ELSE NULL END)) AS display_sugg_posts_count FROM users AS u JOIN user_stats AS us ON u.user_id=us.user_id JOIN posts AS p ON p.user_id=us.user_id LEFT JOIN private AS pv ON pv.post_id=p.post_id AND pv.authorized_user_id='4' WHERE u.user_id='2' GROUP BY u.user_id, us.orig_posts_count, us.sugg_posts_count </code></pre> <p><strong>DETAILED EXPLANATION</strong></p> <p>In your first query, the following posts are being counted (with reasons):</p> <pre><code>display_orig_posts_count = 3: post_id=2: pv.post_id is not null (fails ISNULL() check) post_id=4: pv.post_id is not null (fails ISNULL() check) post_id=6: p.private='0' (fails p.private='1' check) display_sugg_posts_count = 5: post_id=2: pv.post_id is not null (fails ISNULL() check) post_id=3: p.sugg_by='2' (fails p.sugg_by&lt;&gt;'2' check) post_id=4: pv.post_id is not null (fails ISNULL() check) post_id=5: p.sugg_by='2' (fails p.sugg_by&lt;&gt;'2' check) post_id=6: p.private='0' (fails p.private='1' check) </code></pre> <p>In your second query, the following posts are being counted (with reasons):</p> <pre><code>display_orig_posts_count = 1: post_id=2: pv.post_id is not null (fails ISNULL() check) display_sugg_posts_count = 3: post_id=2: pv.post_id is not null (fails ISNULL() check) post_id=3: p.sugg_by='2' (fails p.sugg_by&lt;&gt;'2' check) post_id=5: p.sugg_by='2' (fails p.sugg_by&lt;&gt;'2' check)' </code></pre> <p>In your third query, the following posts are being counted (with reasons):</p> <pre><code>display_orig_posts_count = 2: post_id=4: pv.post_id is not null (fails ISNULL() check) post_id=6: p.private='0' (fails p.private='1' check) display_sugg_posts_count = 2: post_id=4: pv.post_id is not null (fails ISNULL() check) post_id=6: p.private='0' (fails p.private='1' check) </code></pre> <p>What do we want to be counted:</p> <pre><code>display_orig_posts_count = 1: post_id=2: because p.sugg_by=2, p.private=1 AND (is_authorized) display_sugg_posts_count = 3: post_id=4: because p.sugg_by&lt;&gt;'2', p.private=1 AND (is_authorized) post_id=6: because p.sugg_by&lt;&gt;'2', p.private=0 </code></pre> <p>So let's clean this up:</p> <ol> <li><p><strong>Strip out <code>p.user_id='2'</code> from `CASE1 statements</strong></p> <p>This is always true anyway, because you're limiting posts to only those posts with <code>user_id='2'</code> by virtue of your <code>JOIN</code> statements.</p></li> <li><p><strong>Use affirmative checks</strong></p> <p>In the case of <code>display_orig_posts_count</code>, we <em>want</em> certain things. Rather than trying to filter out the things we don't want, let's explicitly look for the things we <em>do</em> want:</p> <pre><code> COUNT(CASE WHEN p.sugg_by='2' AND (p.private='0' OR pv.post_id IS NOT NULL) THEN 1 ELSE NULL ) </code></pre> <p>In the case of <code>display_sugg_posts_count</code>, let's again look for those things explicitly:</p> <pre><code> COUNT(CASE WHEN p.sugg_by&lt;&gt;'2' AND (p.private='0' OR pv.post_id IS NOT NULL) THEN 1 ELSE NULL ) </code></pre></li> <li><p><strong>Use <code>GROUP BY</code> when using aggregation functions</strong></p> <p>You shouldn't need <code>LIMIT 1</code>, either, if you're properly grouping aggregated functions. Only one row should ever be returned for each user.</p></li> </ol>
    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