2015-04-19 72 views
0

我在我的數據庫中有一個表與教練,我已經配置它將體育表的多行添加到一個用戶記錄。查詢需要編輯以允許多個插入

我想配置數據庫,以便我可以一次插入多個教練。

例如,我目前的查詢如下:

INSERT INTO sports (coach, player1, player2, player3, player4) 
SELECT 'maddon', player1, player2, player3, player4 game 
FROM sports 
WHERE coach IN (SELECT id 
       FROM coach_names 
       WHERE coach IN 'pinella','zimmer'); 

我想要做的是什麼,會產生以下結果:

INSERT INTO sports (coach, player1, player2, player3, player4) 
SELECT ('maddon','hernandez','larkin'), player1, player2, player3, player4 game 
FROM sports 
WHERE coach IN (SELECT id 
       FROM coach_names 
       WHERE coach IN 'pinella','zimmer'); 

我所知道的最後一條語句的語法已關閉 - 只是試圖說明我想完成什麼。我寧願只用一個查詢來做到這一點。

我在做的事情基本上是克隆另一位教練員。

任何援助是值得歡迎的。

回答

1

選擇和統一的價值觀作爲一個子表,並與sports表(這裏的MySQL版本)加入吧:

INSERT INTO sports (coach, player1, player2, player3, player4) 
    SELECT new_coach.name, player1, player2, player3, player4 
     FROM sports, (SELECT 'maddon' AS name UNION 
        SELECT 'hernandez' AS name UNION 
        SELECT 'larkin' AS name) as new_coach 
     WHERE coach IN (SELECT id 
         FROM coach_names 
         WHERE coach IN ('pinella','zimmer')); 

看到這個MySQL demo或本Oracle demo