LEFT JOIN teams ON teams.id = (SELECT team_id FROM auth_users WHERE id = offer.user_id)
给我teams表的所有列,但将所有内容都转换为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
提前致谢!