简体   繁体   English

我如何插入到table1中,其中table2.column = value?

[英]How can i insert into Table1 where table2.column = value?

I have a problem basically like the title, the query I have tried is: 我有一个基本上像标题的问题,我尝试过的查询是:

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';

Is there anything wrong with this query, I know that the query without the 这个查询有什么问题吗,我知道没有

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

Works but with the where function i cant seem to get it working 可以但是我在哪里功能似乎无法正常工作

Any ideas? 有任何想法吗? thanks 谢谢

You may try like this: 您可以这样尝试:

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

In this particular case it could be something like this: 在这种情况下,可能是这样的:

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'

You can't do an INSERT ... VALUES... WHERE 您不能在其中INSERT ... VALUES... WHERE

You can do an INSERT .... SELECT ... WHERE 您可以执行INSERT .... SELECT ... WHERE

And you can only INSERT into one table at a time. 而且一次只能INSERT一个表。

So 所以

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

Or 要么

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM