SQLSTATE [42000]:语法错误或访问冲突:1248每个派生表必须具有自己的别名

时间:2012-09-26 20:51:10

标签: mysql

我试图在几个小时内修复这个请求但没有成功,所以我希望你们能帮我找到错误:

SELECT ((
    SELECT DISTINCT *
    FROM (
        SELECT REPLACE(source_name, "+", "") AS name
        FROM events
        WHERE source_name!=""
        UNION
        SELECT REPLACE(target_name, "+", "") AS name
        FROM events
        WHERE target_name!=""
    )
) AS player_name,
(
    SELECT COUNT(*)
    FROM events
    WHERE source_name=player_name AND event_name="beforePlayerBan"
) as nbr_bans,
(
    SELECT COUNT(*)
    FROM events
    WHERE source_name=player_name AND event_name="beforePlayerKick"
) as nbr_kicks,
(
    SELECT COUNT(*)
    FROM events
    WHERE
        (source_name=player_name OR target_name=player_name)
        AND event_name="afterBattleStarted"
) as nbr_battles,
(
    SELECT COUNT(*)
    FROM events
    WHERE source_name=player_name AND result=0
) as nbr_wins,
(
    SELECT COUNT(*)
    FROM events
    WHERE target_name=player_name AND result=0
) as nbr_defeats,
(
    SELECT COUNT(*)
    FROM events
    WHERE (source_name=player_name OR target_name=player_name) AND result=1
) as nbr_ties,
(
    SELECT COUNT(*)
    FROM events
    WHERE (source_name=player_name OR target_name=player_name) AND result=2
) as nbr_forfeits

错误是SQLSTATE [42000]:语法错误或访问冲突:1248每个派生表必须有自己的别名,这很奇怪,因为我确实给每个表都有自己的别名

1 个答案:

答案 0 :(得分:0)

您的第一个子查询似乎缺少别名:

SELECT ((
       SELECT DISTINCT *
       FROM (
              SELECT REPLACE(source_name, "+", "") AS name
              FROM events
              WHERE source_name!=""
              UNION
              SELECT REPLACE(target_name, "+", "") AS name
              FROM events
              WHERE target_name!=""
    )  myMissingAlias -- alias needed here
) AS player_name, 

另外,除非您没有发布完整查询,否则我认为您可能会错过最终)