1

我想将数据插入admissionstudent.

INSERT INTO student(name,age) SELECT name, age from admission

但现在我需要向student:添加另外 2 个字段usernamepasword同时我正在从admission

例子:

INSERT INTO student(name,age,username, password)
  SELECT name, age
    FROM admission.....

我怎样才能做到这一点?

4

3 回答 3

1

你写的查询:

INSERT 
  INTO student(name,age) 
  SELECT name, age from admission

是正确的。SELECT和的行INSERT必须匹配。

但是,我不知道usernameandpassword字段来自哪里。如果他们在同admission一张桌子上,那么,@john Woo 的回答是正确的。如果表不包含这些值,那么您可以使用,

update Student set username=
(select username from <table> and <condition>)
where <condition> 

<table>是包含username字段的表。

<condition>取决于您如何识别行

或者可能join是两个表,即admissions另一个表包含username和并在单个查询中password完成操作insert

INSERT 
  INTO student(name,age,username,password) 
    SELECT a.name, a.age, b.username, b.password
      FROM admission a
      JOIN <table> b ON a.username = b.username
      ....
于 2012-09-06T16:54:15.423 回答
1

我不知道 和 的值username来自password哪里,但您可以创建一个虚拟列(具有默认值,以便与您的INSERT INTO. 例子,

INSERT INTO student(name,age,[username], [password]) 
SELECT name, age, 'userHere' as [username], 'passHere' as [password]
from admission.....
于 2012-09-06T16:44:34.270 回答
0

如果两个新字段都可以为空,您可以执行以下操作:

INSERT INTO student(name,age,username, password)
SELECT name, age, null, null 
FROM admission
于 2012-09-06T19:41:59.403 回答