Jump to content

[SOLVED] INSERT into table every ID from another table?


Recommended Posts

I have a table that I need to insert a row for every ID in another table.

 

Essentially, at regular intervals I need to start a new "round". When this occurs, the "score" table needs to be populated with a row for each player.

 

The tables:

 

CREATE TABLE `Round_Score` (
`Round_Score_ID` int(11) NOT NULL auto_increment,
`Round_ID` int(11) NOT NULL default '0',
`Player_ID` int(11) NOT NULL default '0',
`Raw_Score` decimal(8,3) default NULL,
`Accuracy` decimal(7,4) default NULL,
`Round_Score` int(11) NOT NULL default '0',
PRIMARY KEY  (`Round_Score_ID`),
UNIQUE KEY `Round_ID` (`Round_ID`,`Player_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1


CREATE TABLE `Player_tbl` (
`Player_ID` bigint(20) NOT NULL auto_increment,
`Player_Name` varchar(50) NOT NULL,
PRIMARY KEY  (`Player_ID`),
UNIQUE KEY `Player_Name` (`Player_Name`)
) ENGINE=MyISAM AUTO_INCREMENT=26 DEFAULT CHARSET=latin1

 

So, I am trying to insert every Player_ID from Player_tbl into Round_Score table as its own row.

 

Any help would be really appreciated.

Still haven't figured this one out.

 

I tried this with little expectation that it might work:

 

insert into Round_Score (`Round_Score_ID`,
		`Round_ID`,
		`Player_ID`,
		`Raw_Score`,
		`Accuracy`,
		`Round_Score`
    select 	'NULL', 
	'0',
	Player_ID, 
	'0',
	'0',
	'0'

    from Player_tbl
        left outer join Round_score on Player_tbl.Player_ID=Round_Score.Player_ID;

 

It obviously failed.

Thanks. That's a little embarrassing.

 

Once I got it working I realized that the join was going to insert a row for every ID in the player AND every row in the Round table.

 

I got rid of the join and it works exactly as needed. and is so simple I am a little further embarrassed.

 

This place has been a huge help.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.