2009-07-28 182 views
0

LEFT JOIN队ON teams.id =(SELECT TEAM_ID FROM auth_users WHERE ID = offers.user_id)子查询中左连接

给我的团队表的所有列,但一切都转换为NULL。

LEFT JOIN teams ON teams.id = 1

的作品就像一个魅力

当我做

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

TEAM_ID将是1

由于一些奇怪的原因,它不会在里面JOIN工作。

完整的查询:

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 

提前感谢!

+1

请问您可以发布完整的原始查询(发生在“左连接”之前) – 2009-07-28 12:34:12

+0

1)什么是问题?你给了我们一堆查询,但它可以用来确定你实际上想要做什么。 2)如果没有查询左侧的其余部分,我们不知道我们正在处理什么。 – chaos 2009-07-28 12:34:12

回答

5

首先你应该摆脱所有这些子查询。有了正确的加入,他们都不是真的需要。他们使查询非常拥挤,并且不会影响查询性能。

为了我最好的理解你的查询应该看起来像这样。

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 

接下来,你不应该编写一个分组查询与输出中既不分组也不聚集的字段。我在查询的所有流氓未分组字段上使用了MIN()聚合函数。

我推荐使用短表别名,并且我建议在任何地方使用表别名:例如,作为第三方读者,当涉及表“winner_id”来自哪个表的问题时,我完全丢失了。