2

サブクエリから生成された2つの仮想フィールドを操作して新しいフィールドにしようとしていますが、MySQLは「GP」が不明な列であると言っていますが、すでに宣言されています。私の質問を見てください:

SELECT *,

(SELECT COUNT(id_gol) FROM tb_gol as gol
INNER JOIN tb_jogo as jg ON(gol.fk_id_jogo = jg.id_jogo)
WHERE gol.ic_excluido != '*' AND gol.fk_id_equipe = e.id_equipe AND jg.fk_id_campeonato = g.fk_id_campeonato) as 'GP',

(SELECT COUNT(id_gol) FROM tb_gol as gol
INNER JOIN tb_jogo as jg ON(gol.fk_id_jogo = jg.id_jogo)
WHERE gol.ic_excluido != '*' AND gol.fk_id_equipe != e.id_equipe AND (jg.fk_id_equipe1 = e.id_equipe OR jg.fk_id_equipe2 = e.id_equipe) AND jg.fk_id_campeonato = g.fk_id_campeonato) as 'GC',

(SELECT COUNT(id_wo) as WOs FROM tb_wo as w INNER JOIN tb_jogo as j ON (w.fk_id_jogo = j.id_jogo) WHERE w.fk_id_equipe = e.id_equipe AND j.fk_id_campeonato = g.fk_id_campeonato) as 'WO',

(GP+(GC*-1)) as 'SALDO'

FROM tb_equipe as e
INNER JOIN tb_gruposEquipes as ge ON (e.id_equipe = ge.fk_id_equipe)
INNER JOIN tb_grupos as g ON (g.id_grupo = ge.fk_id_grupo)
WHERE g.fk_id_campeonato = 23
ORDER BY WO ASC

ご覧のとおり、「SALDO」は「GP-GC」の結果です。しかし、MySQLはこれらの列を認識しません

どうすればこれを解決できますか?

解決

助けてくれてありがとう、みんな、しかし私は問題を理解しました。実行時に「GP」と「GC」が存在しないため、GPのGCを差し引いて「SALDO」を作成できませんでした。

したがって、実行時に仮想フィールドを操作する必要がある場合は、仮想フィールドを生成したコードを繰り返す必要があります。

次に、これを選択した場合:

SELECT *, (field1+1) as 'GP', (field2+1) as 'GC', (GP+GC) as 'SALDO' FROM (...)

これに置き換える必要があります:

SELECT *, (field1+1) as 'GP', (field2+1) as 'GC', ((field1+1)+(field2+1)) as 'SALDO' FROM (...)
4

1 に答える 1

1

内部クエリに物をプッシュする

    SELECT core.*,
    (GP+(GC*-1)) as 'SALDO'

    from (

    (SELECT COUNT(id_gol) FROM tb_gol as gol
    INNER JOIN tb_jogo as jg ON(gol.fk_id_jogo = jg.id_jogo)
    WHERE gol.ic_excluido != '*' AND gol.fk_id_equipe = e.id_equipe AND jg.fk_id_campeonato = g.fk_id_campeonato) as 'GP',

    (SELECT COUNT(id_gol) FROM tb_gol as gol
    INNER JOIN tb_jogo as jg ON(gol.fk_id_jogo = jg.id_jogo)
    WHERE gol.ic_excluido != '*' AND gol.fk_id_equipe != e.id_equipe AND (jg.fk_id_equipe1 = e.id_equipe OR jg.fk_id_equipe2 = e.id_equipe) AND jg.fk_id_campeonato = g.fk_id_campeonato) as 'GC',

    (SELECT COUNT(id_wo) as WOs FROM tb_wo as w INNER JOIN tb_jogo as j ON (w.fk_id_jogo = j.id_jogo) WHERE w.fk_id_equipe = e.id_equipe AND j.fk_id_campeonato = g.fk_id_campeonato) as 'WO' ) 


    FROM tb_equipe as e

    ) as core 
    INNER JOIN tb_gruposEquipes as ge ON (core.id_equipe = ge.fk_id_equipe)
    INNER JOIN tb_grupos as g ON (g.id_grupo = ge.fk_id_grupo)
    WHERE g.fk_id_campeonato = 23
    ORDER BY WO ASC
于 2012-12-10T12:23:13.993 に答える