我正在尝试向需要从另一个表中检索值的表中插入额外的行.下面是一个示例查询:
I'm trying to insert additional rows into a table which requires a value to be retrieved from another table. Below is an example query:
insert into a.grades (rollno, grade)
values(select rollno from b.students where ssn=12345, 'A');
b.students
表的结构是rollno, ssn, name
.
我知道上面的查询是错误的.有没有办法在插入行时从其他表中检索 1 个值?
I knew the above query is wrong. Is there a way to retrieve 1 value from other table while inserting a row?
INSERT INTO a.grades (rollno, grade)
SELECT rollno, 'A' FROM b.students WHERE ssn = 12345;
某些 DBMS 会接受以下内容,并在 SELECT 语句周围加上一组额外的括号:
Some DBMS would accept the following, with an extra set of parenthesis around the SELECT statement:
INSERT INTO a.grades (rollno, grade)
VALUES((SELECT rollno FROM b.students WHERE ssn = 12345), 'A');
这篇关于如何在 MySQL 的“插入"语句中使用“选择"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!