使用select进行sql insert查询

时间:2015-04-14 12:21:29

标签: mysql select insert

根据此表,reviewidprankid都是自动递增的。

CREATE TABLE Review
(
reviewId INT NOT NULL AUTO_INCREMENT,
email VARCHAR(32) NOT NULL,
prankId INT NOT NULL,
rating INT,
comment VARCHAR(1056) NOT NULL,
PRIMARY KEY(reviewId),
FOREIGN KEY (email) REFERENCES User(email),
FOREIGN KEY (prankId) REFERENCES Prank(prankId)
);

此insert语句是否会正确地将值插入审阅表中的所有属性。

INSERT INTO Review (email, prankId) SELECT email, prankId from User;
INSERT INTO Review (rating, comment) VALUES(‘5’,’amazing!’);
INSERT INTO Review (rating, comment) VALUES(‘5’,’brilliant!’);

1 个答案:

答案 0 :(得分:0)

我怀疑你想要这个:

INSERT INTO Review (email, prankId, rating, comment)
    SELECT email, prankId, 'S', 'amazing!'
    from User;

INSERT INTO Review (email, prankId, rating, comment)
    SELECT email, prankId, 'S', 'brilliant'
    from User;

在您的版本中,最后两个insert将失败,因为emailprankidNULL。请记住,insert会在表格中添加新行。它不会修改现有的行。