如何在table1中插入table2.column = value?

时间:2012-09-02 16:09:50

标签: php sql

我的问题基本上就像标题一样,我试过的查询是:

INSERT INTO 
  pendingresults, 
  l1_afixtures (pendingresults.TeamAName, pendingresults.TeamA, 
pendingresults.TeamAScore, pendingresults.TeamBScore, pendingresults.TeamB) 
VALUES (
  '$pls', 
  '$TeamA', 
  '$TeamAScore', 
  '$TeamBScore', 
  '$TeamB') 
WHERE 
  l1_afixtures.team_name = $TeamA 
AND  
  l1_afixtures.fixture = $TeamB 
AND 
  l1_afixtures.disabled = 'enabled';

这个查询有什么问题,我知道没有

的查询
 where l1_afixtures.team_name = $TeamA AND  
 l1_afixtures.fixture = $TeamB AND l1_afixtures.disabled = 'enabled';

工作但是使用where功能我似乎无法使其正常工作

有什么想法吗?感谢

2 个答案:

答案 0 :(得分:2)

您可以尝试这样:

INSERT INTO Table2 (<columns>)
SELECT <columns>
FROM Table1
WHERE <condition>;

在这种特殊情况下,它可能是这样的:

INSERT INTO pendingresults (TeamAName, TeamA, TeamAScore, TeamBScore, TeamB) 
SELECT TeamAName, TeamA, TeamAScore, TeamBScore, TeamB
FROM l1_afixtures
WHERE 
    l1_afixtures.team_name = $TeamA 
    AND l1_afixtures.fixture = $TeamB 
    AND l1_afixtures.disabled = 'enabled'

答案 1 :(得分:-2)

您无法执行INSERT ... VALUES... WHERE

您可以执行INSERT .... SELECT ... WHERE

您一次只能INSERT进入一个表格。

所以

insert into pendingresults (TeamAName, TeamA, TeamAScore, TeamBScore, TeamB) 
values('$pls', '$TeamA', '$TeamAScore', '$TeamBScore', '$TeamB') 

或者

insert into pendingresults (TeamAName, TeamA, TeamAScore, TeamBScore, TeamB) 
Select TeamAName, TeamA, TeamAScore, TeamBScore, TeamB
from l1_afixtures
where 
    l1_afixtures.team_name = $TeamA 
    AND l1_afixtures.fixture = $TeamB 
    AND l1_afixtures.disabled = 'enabled'