数据库 insert into from 写到 values 里

Image for post
Image for post

低水平文章警告

先来看个例子

Table1
No Name
1 Andy
2 Bob
3 Cathey

Table2
No Score(int)
1 85
2 98
3 95

在往 Table2 里写数据的话,最简单的可以这样:

insert into Table(No, Score) values (“1”, 85);
insert into Table(No, Score) values (“2”, 98);
insert into Table(No, Score) values (“3”, 95);

那如果 Table2 中的 No 值想引用 Table 1 的值呢,除了在弄个临时表中转一下,也可以直接把 insert into from 写到 insert into values 里。比如第一条可以这样写:

insert into Table(No, Score) values ((select No from Table1 where NAME=”Andy”), 85);

括号一定要加,这是一个小坑点。

Written by

最后的栖息地

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store