2014-04-19 50 views
2

我表1包含:插入來自其他表的多個行 - 「子查詢返回多個1行」

|col1| 
    | 1 | 
    | 2 | 
    | 1 | 
    | 3 | 
    | 1 | 
    | 2 | 
    | 4 | 
    | 2 | 
    | 3 | 
    | 1 | 

,我有另一個表有一欄名稱val,和我的代碼是

INSERT INTO table2(value) VALUES ((select distinct col1 from table1)) 

我得到了#1242 - 子查詢返回多於1行。
如何獲取多行插入到我的表2中?

+0

的可能重複[插入與子查詢多個值表(http://stackoverflow.com/questions/13532162 /插入 - 進入 - 表與 - 多值功能於子查詢) – user2864740

回答

3

使用insert . . . select時,您不需要values聲明:

INSERT INTO table2(value) 
    select distinct col1 
    from table1; 
4
INSERT INTO table2(value) 
select distinct col1 
from table1