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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:36:40+00:00 2026-05-12T06:36:40+00:00

LEFT JOIN teams ON teams.id = (SELECT team_id FROM auth_users WHERE id = offers.user_id)

  • 0

LEFT JOIN teams ON teams.id = (SELECT team_id FROM auth_users WHERE id = offers.user_id)

Gives me all the columns of the teams table, but converts everything to NULL.

LEFT JOIN teams ON teams.id = 1

works like a charm

When i do

SELECT (SELECT team_id FROM auth_users WHERE id = offers.user_id) as team_id

team_id will be 1.

For some strange reason it does not work inside the JOIN.

Full Query:

SELECT projects.id, projects.title as title, winner_id, projects.user_id as user_id,   until, pages, types.title as type, types.id as type_id, projects.id as id, offers.price as price, offers.delivery_date as delivery_date, teams.*,
(SELECT COUNT(id) FROM comments WHERE comments.project_id = projects.id AND comments.private = 1) as comments,
(SELECT COUNT(id) FROM uploads WHERE uploads.project_id = projects.id) as files,
(SELECT country FROM auth_users WHERE auth_users.id = offers.user_id) as baser_country,
(SELECT business FROM auth_users WHERE auth_users.id = offers.user_id) as baser_business,
(SELECT CONCAT(firstname, ' ', lastname) FROM auth_users WHERE auth_users.id = offers.user_id) as baser_name,
(SELECT team_id FROM auth_users WHERE id = offers.user_id) as team_id,
(SELECT country FROM auth_users WHERE auth_users.id = projects.user_id) as customer_country,
(SELECT business FROM auth_users WHERE auth_users.id = projects.user_id) as customer_business
FROM projects
JOIN types ON projects.type_id = types.id
LEFT JOIN offers ON projects.id = offers.project_id
LEFT JOIN teams ON teams.id = (SELECT team_id FROM auth_users WHERE id = offers.user_id)
WHERE projects.user_id = 1 AND winner_id != 0 AND uploaded = 1
GROUP BY projects.id
ORDER BY projects.id DESC
LIMIT 3

Thanks in advance!

  • 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-12T06:36:40+00:00Added an answer on May 12, 2026 at 6:36 am

    First you should get rid of all these sub-queries. With the correct join, none of them is really necessary. They make the query very crowded, and they do not contribute to query performance.

    To my best understanding your query should look something like this.

    SELECT 
      p.id                 AS project_id, 
      MIN(p.title)         AS project_title, 
      MIN(winner_id)       AS winner_id, 
      MIN(p.user_id)       AS project_user_id,
      MIN(until)           AS until, 
      MIN(pages)           AS pages, 
      MIN(t.id)            AS type_id, 
      MIN(t.title)         AS type_title, 
      MIN(o.price)         AS offer_price, 
      MIN(o.delivery_date) AS offer_delivery_date, 
      -- m.*, <-- this should be avoided, especially in a grouped query
      COUNT(c.id)          AS count_comments,
      COUNT(u.id)          AS count_files,
      MIN(ao.country)      AS baser_country,
      MIN(ao.business)     AS baser_business,
      MIN(CONCAT(ao.firstname, ' ', ao.lastname)) AS baser_name,
      MIN(ao.team_id)      AS baser_team_id,
      MIN(ap.country)      AS customer_country,
      MIN(ap.business)     AS customer_business
    FROM 
      projects p
      INNER JOIN types       t ON p.type_id    = t.id
      LEFT  JOIN offers      o ON o.project_id = p.id
      LEFT  JOIN comments    c ON c.project_id = p.id AND comments.private = 1
      LEFT  JOIN uploads     u ON u.project_id = p.id
      LEFT  JOIN auth_users ao ON ao.id = o.user_id
      LEFT  JOIN auth_users ap ON ap.id = p.user_id
      LEFT  JOIN teams       m ON m.id  = o.team_id
    WHERE 
      p.user_id = 1 
      AND winner_id != 0 
      AND uploaded = 1
    GROUP BY 
      p.id
    ORDER BY 
      p.id DESC
    LIMIT 3
    

    Next you should never write a grouped query with fields in the output that are neither grouped nor aggregated. I’ve used the MIN() aggregate function on all the rogue ungrouped fields of your query.

    I recommend the use of short table aliases and I recommend using the table aliases everywhere: As a third party reader I’m completely lost when it comes to the question which table “winner_id” comes from, for example.

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

Sidebar

Related Questions

In my Web Application I load many Entities, to display them in a table.
The following query takes 25 seconds with 2 table joins. The first posts table
I have 2 tables, and I would like to join them using Lambda statements
Is it possible to do something along those lines: DELETE TOP(1) m FROM MyTable
I can't make a where clause on the IdCell column but i can do
I try to avoid subqueries due to the fact they usually have much lower
I'm trying to optimize some of my selects using the explain analyze, and I
I have the trigger: create or replace TRIGGER JACKET_DELETE BEFORE DELETE ON JACKET FOR
As addition to this question, I took Ladislav Mrnka's answer . The problem is
Fairly new to mysql, and I am lost at the moment. This is my

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.