以下命令用于将数据从一个表插入另一个表。
insert into school_record (phone_id)
select phone_id from students where name="abc"
order by phone_id asc;
但是如果我想插入名称的所有phone_id值" abc"," def"," ghi"等等...
在从多个记录中选择值后,如何将数据从一个表插入另一个表?
答案 0 :(得分:2)
如果您想要学生表的所有phone_ids,您可以这样做:
insert into school_record (phone_id) select phone_id from students order by phone_id asc;
答案 1 :(得分:2)
只有WHERE子句限制了记录数。做到这一点
where Name = 'abc' or name = 'def' or name = 'ghi'
或
where name in ('abc','def','ghi')
答案 2 :(得分:2)
如果要插入具有常量名称的数据,可以尝试以下方法:
insert into school_record (phone_id)
select phone_id from students where name in ("abc","def","xyz")
order by phone_id asc;
如果您想为其他表格中包含的名称插入phone_id,请尝试以下操作:
insert into school_record (phone_id)
select phone_id from students where name in (select name from tablename)
order by phone_id asc;
参考"在"从下面: https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in
答案 3 :(得分:1)
使用IN() - 在括号中列出您需要插入的所有名称
insert into school_record (phone_id)
select phone_id
from students
where name in ('abc', 'def', 'ghi', ...)
;
或者如果您需要插入所有
insert into school_record (phone_id)
select phone_id
from students
;