有人能告诉我为什么这不起作用吗?
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 "');"
答案 0 :(得分:22)
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'
);
如果子查询返回多个值,这一点尤为重要。然后查询可能会出错。
答案 1 :(得分:-1)
我遇到语法错误,因为我忘记了'治疗'上的引号('
)。