15

有人能告诉我为什么这不起作用吗?

INSERT INTO Medical_History (pid, grafts, allergies,  diseases, surgearies, treatment)
VALUES ((SELECT pid FROM Pet WHERE pet_name='Jack' AND cid=(SELECT cid FROM Customer WHERE last_name='Iwannidis' AND first_name='Giwrgos')),
       'grafts', 'allergies', 'diseases', 'surgearies', treatments');

我收到语法错误:

unrecognized token "');"
4

2 回答 2

27

语句中的select嵌套values看起来是错误的(我不确定所有数据库都接受它)。更典型的表达方式是:

INSERT INTO Medical_History (pid, grafts, allergies,  diseases, surgearies, treatment)
    SELECT pid, 'grafts', 'allergies', 'diseases', 'surgearies', 'treatments'
    FROM Pet
    WHERE pet_name='Jack' AND
          cid=(SELECT cid
               FROM Customer
               WHERE last_name='Iwannidis' AND first_name='Giwrgos'
              );

如果子查询返回多个值,这一点尤其重要。那么查询很可能会出错。

于 2013-05-28T01:35:36.783 回答
1

我有一个语法错误,因为我忘记了'“治疗”上的引号 ( )。

于 2013-05-28T01:36:59.133 回答