Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8571395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:46:20+00:00 2026-06-11T18:46:20+00:00

SELECT postID, threadID, COUNT(DISTINCT postID) as count FROM wbb1_1_post post LEFT JOIN wcf1_user_to_groups groups

  • 0
SELECT postID, threadID, COUNT(DISTINCT postID) as count
FROM wbb1_1_post post
LEFT JOIN wcf1_user_to_groups groups
ON post.userID = groups.userID
WHERE threadID IN (468)
AND groupID IN (4,10)
AND isDisabled != 1
AND isDeleted != 1
ORDER BY postID ASC

Chris = groupID 4

Sebastian S. = groupID 10

Result:

 postID  threadID  count 
  2811     468      2

Everything is correct.

enter image description here

SELECT postID, threadID, COUNT(DISTINCT postID) as count
FROM wbb1_1_post post
LEFT JOIN wcf1_user_to_groups groups
ON post.userID = groups.userID
WHERE threadID IN (68)
AND groupID IN (7)
AND isDisabled != 1
AND isDeleted != 1
ORDER BY postID ASC

talishh and Bender = groupID 7

Result:

postID  threadID    count
349 (wrong!) 68        3

The result should be 308, cause I want the oldest post (that’s why I ORDER by postID ASC).
enter image description here
Is there a way to optimize this query?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:46:21+00:00Added an answer on June 11, 2026 at 6:46 pm

    It’s because mysql it’s really generous with aggregate functions (COUNT, MIN…) and grouping.
    The bad part : you ain’t got control about how it groups…
    And ORDER BY doesn’t seem to correct the problem in your case.

    Other DBMS (and ANSI SQL) force you to group the fields which aren’t in aggregate functions, when you use one. Looks boring, but “you know what you get”.

    In your case, simply use the MIN on PostID and GROUP BY threadID should do the trick.

    SELECT MIN(postID), threadID, COUNT(*) as count
    FROM wbb1_1_post post
    LEFT JOIN wcf1_user_to_groups groups
    ON post.userID = groups.userID
    WHERE threadID IN (68)
    AND groupID IN (7)
    AND isDisabled != 1
    AND isDeleted != 1
    group by threadID
    

    by the way, your LEFT JOIN doesn’t make sense (if I’m not wrong) as you use your joined entity in a where statement.

    And no need to use IN if you put only one value, but I guess this is just sample code.

    And no need to use COUNT(distinct postID) with a correct grouping. Just use COUNT(*)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

table: postid|userid|post|replyto post sql SELECT * FROM table WHERE postid=12 total replies sql SELECT
SELECT * From `users` AS `User` LEFT JOIN `selections` AS `Selections` ON (`Selections`.`user_id` =
I need help converting this SQL statement, into EF4: Select Posts.PostID, Post, Comment from
SELECT POSTS.* FROM POSTS LEFT JOIN SUBSCRIBERS ON POSTS.AUTHORID = SUBSCRIBERS.PROFILEID WHERE POSTS.AUTHORID =
My query is: SELECT temp.pid FROM (SELECT postid, date FROM swapping AS s, post
If I have this SQL query: select distinct top 1 'PostId' = isnull(RootPost,Id), PostedDateTimeUtc
I have a basic MySQL query: $getFeed = SELECT posts.postID, posts.postTitle, ..... FROM posts
I have this: Post post = GetPost(postID); if (post.User.UserID == userID) return true; And
How to convert this one to LINQ? select d.UserID, d.Content, d.UpdateTime from DiaryPosts as
SELECT *, (SELECT SUM(rating) FROM votes WHERE votes.postId = posts.id) AS rating FROM posts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.