1

我正在使用嵌套查询从表中获取我需要的值,然后我需要在条件语句中使用该值,但是,每次我尝试这个时,我都会收到一个错误,指出字段列表中的未知列(格式)

SELECT

(SELECT format FROM competition_stages WHERE comp_id = "5" AND rid = "24") AS format,

a.tie_id, b.name AS team_a, b.team_id AS team_a_id, c.name AS team_b, c.team_id AS team_b_id, SUM(e.bonus) AS team_a_bonus, SUM(f.bonus) AS team_b_bonus,

SUM(CASE 
    WHEN (a.team_a = e.team_id AND format = "0") THEN e.score
END) as team_a_agg,

SUM(CASE 
    WHEN (a.team_b = f.team_id AND format = "0") THEN f.score
END) as team_b_agg

FROM competition_tie a

INNER JOIN teams b ON (a.team_a = b.team_id)
INNER JOIN teams c ON (a.team_b = c.team_id)

LEFT JOIN fixtures d ON (a.tie_id = d.tie_id)
LEFT JOIN fixture_scores e ON (d.fx_id = e.fx_id AND a.team_a = e.team_id)
LEFT JOIN fixture_scores f ON (d.fx_id = f.fx_id AND a.team_b = f.team_id)

WHERE a.comp_id = "5" AND a.rid = "24" AND a.season_id = "5"

GROUP BY a.tie_id

ORDER BY a.tie_id ASC

当我浏览它们时,我可以在结果中获取格式列的值,但似乎我无法在查询中使用它。

谢谢你的帮助!

4

1 回答 1

0

无需使用子查询,只需将 Competition_stages 表连接到您的查询,以便您可以直接引用格式列。假设竞争阶段表和查询中的其他表之间没有明显的关系(至少不是来自手头的信息),您可以使用您指定的两个条件加入表,如果这些条件只会产生 1 个结果比赛阶段表。像这样的东西:

SELECT cs.format, a.tie_id, ....
FROM competition_tie a ...
INNER JOIN competition_stages cs ON cs.comp_id = "5" AND cs.rid = "24"
于 2013-04-16T19:33:26.933 回答