sql错误,此类型的子句以前被解析过

时间:2017-12-01 04:08:23

标签: php mysql

我尝试编写php文件以将数据插入到我的数据库中。但我得到了错误。

此类型的子句先前已被解析。 (位置233处的“FROM”附近) 无法识别的语句类型。 (位置233处的“FROM”附近)

这是代码

INSERT INTO Game(date,teamone_id,teamtwo_id) SELECT
            '$array[4]',
            tid 
            FROM Team 
            WHERE teamName='$array[2]'
            OR teamName='$array[3]',    
            tid 
            FROM Team 
            WHERE teamName='$array[2]'
            OR teamName='$array[3]'

1 个答案:

答案 0 :(得分:0)

您不能拥有多个WHERE条款。看起来你正试图获得一个交叉产品,你需要使用自我加入。

INSERT INTO Game (date, teamone, teamtwo)
SELECT '$array[4]', t1.tid, t2.tid
FROM Team AS t1
CROSS JOIN Team AS t2
WHERE t1.teamName = '$array[2]'
AND t2.teamName = '$array[3]'
AND NOT EXISTS (
    SELECT 1
    FROM Game AS g
    JOIN Team AS t1 ON t1.tid = g.teamtwo
    JOIN Team AS t2 ON t2.tid = g.teamone
    WHERE t1.teamName = '$array[2]'
    AND t2.teamName = '$array[3]'
    AND g.date = '$array[4]'
)

NOT EXISTS检查现有行,并且团队ID已反转。