簡體   English   中英

我們如何從一個表到另一個表插入“來自多個記錄的數據”?

[英]How do we insert “data from more than one records”, from one table to another?

以下命令用於將數據從一個表插入另一個表。

insert into school_record (phone_id)
    select phone_id from students where name="abc"
    order by phone_id asc;

但是,如果我想為名稱“ abc”,“ def”,“ ghi”等插入所有phone_id值,該怎么辦...

從多個記錄中選擇值后,如何將數據從一個表插入到另一表?

如果您要獲取students表的所有phone_id,則只需執行以下操作:

insert into school_record (phone_id) select phone_id from students  order by phone_id asc;

僅您的WHERE子句限制記錄的數量。 做了

where Name = 'abc' or name = 'def' or name = 'ghi'

要么

where name in ('abc','def','ghi')

如果要使用常量名稱插入數據,則可以嘗試以下操作:

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

使用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 
;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM