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 744845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T09:01:32+00:00 2026-05-14T09:01:32+00:00

I seem to remember reading somewhere that there is a way to combine LEFT

  • 0

I seem to remember reading somewhere that there is a way to combine LEFT JOIN statements into a more simple query. I have the following query and was hoping someone could kindly take a look at it.

SET @userId = 8;

SELECT ug.user_id, COUNT(DISTINCT goal_id) as matches
FROM user_goal ug, user u, profile p
LEFT JOIN user_block ub ON @userId = ub.blocked_id
LEFT JOIN user_block ub2 ON @userId = ub2.blocker_id
LEFT JOIN user_user uu ON @userId = uu.user_id
LEFT JOIN friend_request fr ON @userId = fr.user_id
WHERE ug.user_id = u.id AND u.profile_id = p.id
AND (ub.blocker_id IS NULL OR ub.blocker_id != ug.user_id)
AND (ub2.blocked_id IS NULL OR ub2.blocked_id != ug.user_id)
AND (uu.user_friends_id IS NULL OR uu.user_friends_id != ug.user_id)
AND (fr.to_user_id IS NULL OR (fr.to_user_id != ug.user_id))
AND ug.user_id!=@userId
AND p.suggestible
AND goal_id IN (SELECT iug.goal_id FROM user_goal iug WHERE user_id=@userId)
GROUP BY user_id ORDER BY matches DESC LIMIT 4
  • 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-05-14T09:01:32+00:00Added an answer on May 14, 2026 at 9:01 am

    You can improve this overall query by removing that @userId to start with 😉 You can just reference previously mentioned columns to maintain that join.

    This may then pose a problem with your subquery; which does not need to be there. That subquery will almost certainly be causing slow query execution here. You can incorporate the subquery into a derived table that you’re also joining against (since it’s using the same user_id), this way it only executes once, rather than once for each record in the resultset as is happening now.

    SELECT
      ug.user_id,
      COUNT(DISTINCT goal_id) as matches
    FROM
      user_goal ug
      INNER JOIN user u ON ug.user_id = u.id
      INNER JOIN profile p ON u.profile_id = p.id
      LEFT JOIN user_block ub ON u.id = ub.blocked_id
      LEFT JOIN user_block ub2 ON u.id = ub2.blocker_id
      LEFT JOIN user_user uu ON u.id = uu.user_id
      LEFT JOIN friend_request fr ON u.id = fr.user_id
      LEFT JOIN (
        SELECT iug.goal_id FROM user_goal
      ) iug ON iug.user_id = u.id
    WHERE
      /* I looked at your WHERE clause for a while and couldn't get my head
         around what you're trying to do so I'll leave this to you */
    

    Putting the sub-query directly into a JOIN and naming it creates a derived table, really speeding things up (and tidying things up IMHO).

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

Sidebar

Related Questions

I seem to remember reading somewhere that google app engine automatically caches the results
I vaguely remember reading somewhere (in MSDN ODBC documentation?) that one application cannot make
I seem to remember reading about a function that could be used to make
I seem to remember that there is a command to look at a mysql
I seem to remember reading something about how it is bad for structs to
I seem to remember there is a problem with WITH. I don’t miss it;
I seem to remember that Regular Expressions in DotNet have a special mechanism that
I seem to remember seeing this somewhere, but for the life of me search
I seem to remember a tool that could be used at runtime to tell
In .net 1.1 I seem to remember I had to declare web controls that

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.