Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with query optimization
    primarykey
    data
    text
    <p>I had two queries - one that selects <code>group_id</code> by knowing the <code>post_id</code>, and one that checks that user is in that group. I tried to optimize them - put them together, but it now throws an error...</p> <pre><code>SELECT COUNT(bio_community_group_members.id) AS count FROM `bio_community_group_members` JOIN `bio_community_posts` ON (`bio_community_posts`.`id` = `180`) WHERE `bio_community_group_members`.`group_id` = 'bio_community_posts.group_id' AND `bio_community_posts`.`user_id` = '34' </code></pre> <p>It says:</p> <blockquote> <p>Unknown column '180' in 'on clause'.</p> </blockquote> <p>Problem: I have that entry!</p> <p>Table structure:</p> <p><code>bio_community_posts</code>:</p> <ul> <li><code>id</code>,</li> <li><code>user_id</code>,</li> <li><code>group_id</code>,</li> <li>other stuff;</li> </ul> <p><code>bio_community_group_members</code>:</p> <ul> <li><code>id</code>,</li> <li><code>user_id</code>,</li> <li><code>group_id</code>,</li> <li><code>status</code>,</li> <li>other stuff;</li> </ul> <p>I need to retrieve <code>status</code> from <code>bio_community_group_members</code> if it exists. That <code>count</code> thing was just because I didn't knew how to start to build my query. :(</p> <p>Thanks in advice.</p> <p><strong>Edit:</strong></p> <p>Hmm... now it works..... but any idea how to optimize those two queries and get one that also selects <code>status</code>?</p> <pre><code>SELECT `group_id` FROM `bio_community_posts` WHERE `id` = 180 SELECT COUNT(id) AS count FROM `bio_community_group_members` WHERE `group_id` = 41 AND `user_id` = '34' </code></pre> <p><strong>Edit #2:</strong></p> <p>This is what I was looking for:</p> <pre><code>SELECT `bio_community_group_members`.`status` FROM `bio_community_group_members` JOIN `bio_community_posts` ON `bio_community_posts`.`group_id` = `bio_community_group_members`.`group_id` WHERE `bio_community_group_members`.`group_id` = 41 AND `bio_community_group_members`.`user_id` = '34' AND `bio_community_posts`.`id` = '180' GROUP BY `bio_community_group_members`.`status` </code></pre> <p>Thanks! :)</p> <p><strong>Edit #3:</strong></p> <p>I guess that i need something like this...</p> <pre><code>SELECT `bio_community_group_members`.`status` FROM `bio_community_group_members` JOIN `bio_community_posts` ON `bio_community_posts`.`group_id` = `bio_community_group_members.group_id` WHERE `bio_community_posts`.`id` = '180' AND `bio_community_posts`.`user_id` = '34' </code></pre> <p>But:</p> <blockquote> <p>[Err] 1054 - Unknown column 'bio_community_group_members.group_id' in 'on clause'.</p> </blockquote> <p><strong>Edit #4:</strong></p> <p>Just found a bug in the query. Here is the ultimate solution:</p> <pre><code>SELECT `bio_community_group_members`.`status` FROM `bio_community_group_members` JOIN `bio_community_posts` ON `bio_community_posts`.`group_id` = `bio_community_group_members`.`group_id` WHERE `bio_community_posts`.`id` = '180' AND `bio_community_posts`.`user_id` = '34' </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.
 

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