简体   繁体   English

sql插入查询与选择

[英]sql insert query with select

Given this table where both reviewid and prankid are auto increment. 给定该表, 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)
);

Would this insert statement correctly insert values into all of the attributes in review table. 此插入语句是否可以将值正确插入检查表中的所有属性中。

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!’);

I suspect that you want this: 我怀疑您想要这样:

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;

In your version, the last two insert s will fail because email and prankid are NULL . 在您的版本中,最后两个insert将会失败,因为emailprankidNULL Remember, insert adds new rows into the table. 请记住, insert将新行添加到表中。 It doesn't modify existing rows. 它不会修改现有行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM