sql插入多行内部选择

时间:2012-01-07 07:17:19

标签: mysql sql insert

我需要插入多行,但有些数据必须来自内部选择 我有问题构建正确的sql语句,例如我有:

insert into game_friends(
game_users_id,
game_users_id_name,
created_to_app_users_id,
created_to_app_users_id_name
) VALUES  ......

现在有问题的部分是created_to_app_users_id_name和game_users_id_name我只能通过使用这样的选择获得:

SELECT app_users_game_users_id_name
FROM `app_users` WHERE app_users_game_users_id = $game_users_id

SELECT app_users_created_to_app_users_id_name  
FROM `app_users` 
WHERE app_users_created_to_app_users_id = $created_to_app_users_id

如何使用mysql将它与一个sql语句结合起来

更新
感谢所有回答,但我想没有解释我的问题吧... 我需要插入多行,这意味着我将有5-7个game_users_id来 它最终需要看起来像(在这里选择..)

insert into game_friends(
    game_users_id,
    game_users_id_name,
    created_to_app_users_id,
    created_to_app_users_id_name
    )  
VALUES
  ($game_users_id, app_users_created_to_app_users_id_name, $created_to_app_users_id,app_users_created_to_app_users_id_name ),
  ($game_users_id, app_users_created_to_app_users_id_name, $created_to_app_users_id,app_users_created_to_app_users_id_name ),
  ($game_users_id, app_users_created_to_app_users_id_name, $created_to_app_users_id,app_users_created_to_app_users_id_name ),
  ($game_users_id, app_users_created_to_app_users_id_name, $created_to_app_users_id,app_users_created_to_app_users_id_name );

其中每个Values条目都需要由select组成。

3 个答案:

答案 0 :(得分:0)

尝试PHP代码:

$query = "insert into game_friends(
 game_users_id,
 game_users_id_name,
 created_to_app_users_id,
 created_to_app_users_id_name
) SELECT
 app_users_game_users_id,
 app_users_game_users_id_name,
 app_users_created_to_app_users_id,
 app_users_created_to_app_users_id_name
FROM `app_users` WHERE
app_users_game_users_id = $game_users_id
AND
app_users_created_to_app_users_id = $created_to_app_users_id";

答案 1 :(得分:0)

这是UNTESTED

insert into game_friends
(
    game_users_id,
    game_users_id_name,
    created_to_app_users_id,
    created_to_app_users_id_name
) 
VALUES
SELECT  game_users_id, 
        created_to_app_users_id, 
        FinalTable.game_users_id_name,
        FinalTable.created_to_app_users_id_name
FROM
    ((SELECT app_users_game_users_id as game_users_id,
           app_users_game_users_id_name as game_users_id_name
    FROM   app_users
    WHERE  app_users_game_users_id = game_users_id) as iGameID
        INNER JOIN
    (SELECT app_users_created_to_app_users_id as created_to_app_users_id,
           app_users_created_to_app_users_id_name as created_to_app_users_id_name
    FROM   app_users
    WHERE  app_users_created_to_app_users_id = created_to_app_users_id) as iCreatedID
        ON iGameID.game_users_id = iCreatedID.created_to_app_users_id)  as FinalTable

答案 2 :(得分:0)

简单SELECT超过两个没有任何JOINs的表可以做到这一点(假设每个select语句只返回一行):

INSERT INTO game_friends (
    game_users_id,
    game_users_id_name,
    created_to_app_users_id,
    created_to_app_users_id_name
)

SELECT  u_game.app_users_game_users_id,
        u_game.app_users_game_users_id_name,
        u_created.app_users_created_to_app_users_id,
        u_created.app_users_created_to_app_users_id_name
FROM    `app_users` u_game,
        `app_users` u_created
WHERE   u_game.app_users_game_users_id = $game_users_id
    AND u_created.app_users_created_to_app_users_id = $created_to_app_users_id

另一个注意事项:我猜你app_users表确实有一列app_users_game_users_idapp_users_created_to_app_users_id。在这种情况下,您应该使用实际列名替换SQL中的那些,我想这可能是user_idapp_user_idid。只是假设上面提到的两个列在表中都是唯一的,你的模型看起来很奇怪。