SQL INSERT里面有SELECT,也许是内连接

时间:2013-06-18 19:27:46

标签: php mysql sql

我有这个INSERT:

INSERT INTO gamecodes (gamecode, accountname, premium_points, alreadyused)
VALUES ('$gamecode','$accountorname',$premiumpoints,'N')

我需要帮助。在变量 $ accountorname 中,我有一个玩家名字,我需要获得他的帐户。

两张桌子:

TABLE PLAYERS
id, name, account_id..
id, player name, table accounts.id

TABLE ACCOUNTS
id, name ..
id = table players account_id

2 个答案:

答案 0 :(得分:0)

您的查询可能类似于 -

INSERT INTO gamecodes( gamecode, accountname, premium_points, alreadyused) 
 VALUES('$gamecode', '$accountname', $premiumpoints, 'N')
 WHERE(SELECT accountname FROM accounts JOIN gamecodes ON accounts.accountname = gamecodes.accountname WHERE accounts.accountname = $accountname)

答案 1 :(得分:0)

您可以尝试以下方式:

INSERT INTO gamecodes (gamecode, accountname, premium_points, alreadyused) 
SELECT '$gamecode',accounts.name,$premiumpoints,'N' 
FROM accounts 
JOIN players
ON accounts.id = players.account_id 
WHERE players.name = '$accountorname'