查询需要编辑以允许多个插入

时间:2015-04-19 19:12:36

标签: sql mysqli sql-insert

我的数据库中有一个包含教练的表格,我已将其配置为将多个体育表格添加到一个用户记录中。

我正在尝试配置数据库,这样我就可以同时插入多个教练。

例如,我当前的查询如下:

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 个答案:

答案 0 :(得分: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