简体   繁体   中英

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

You can do an INSERT .... SELECT ... WHERE

And you can only INSERT into one table at a time.

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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