2014-05-21 32 views
-1

如上標題填充數據,我想從一個表中插入值到另一個困難來自另一個表

我跟着在這裏建議的答案, Need to populate one table with data from another table in same database

MySQL的代碼止跌回升

INSERT INTO `profitdb`(`BetID`, `PlayerID`, `PlayerWL`) SELECT `betdb`.`BetID`, `betdb`.`PlayerID`,`betdb`.`BetAmount` FROM `betdb` LEFT OUTER JOIN `profitdb` ON `betdb`.`BetID` = `profitdb`.`BetID` 

,但我遇到這個錯誤

1064 - You have an error in your SQL syntax; check the manual that corresponds to 
your MySQL server version for the right syntax to use near 
'SELECT `betdb`.`BetID`, `betdb`.`PlayerID`,`betdb`.`BetAmount` FROM `betdb` 
LE' at line 3 

這可能是我的錯嗎?

+0

你的查詢是錯誤的,你不需要部分'VALUES(....)' –

+1

既可以使用'VALUES'或'SELECT'作爲'INSERT',而不是兩者,除此之外,你正在使用''betdb ''在你的'FROM'和'JOIN'中沒有別名,爲其中至少一個引入一個別名 – DrCopyPaste

+0

如果你做一個選擇插入你不需要值 – Daniele94

回答

0

你做INSERT INTO TABLE A(field1,2,3) VALUES(1,2,3),然後執行SELECT所以錯誤意味着你應該做的INSERT ... VALUES .... **;** SELECT執行2個查詢,但你真正想要的是:

INSERT INTO `profitdb`(`BetID`, `PlayerID`, `Outcome`) 
SELECT `betdb`.`BetID`, `betdb`.`PlayerID`,`betdb`.`BetAmount` FROM `betdb` 

從一個表中的數據複製到另一個,閱讀: http://dev.mysql.com/doc/refman/5.6/en/insert-select.html

+0

哦,我看到了,非常感謝 – hikki

+0

另一個問題,我有新的列將來自其他表,我將如何寫查詢?讓我們說要獲得'AgentRisk',你需要從'Agents'表得到它'SubAgentRisk'的相同,所以像INSERT INTO'profitdb'('BetID','PlayerID','Outcome','AgentRisk' ,'SubAgentRisk') SELECT'betdb'.'BetID','betdb'.'PlayerID','betdb'.'BetAmount' FROM'betdb'選擇'風險'從'agentdb'選擇'風險'從'subagentdb' – hikki