Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?



SQL query required for moving data


6 replies to this topic - - - - -

#1 marklcfc

    Neowinian²

  • 184 posts
  • Joined: 30-March 05

Posted 10 May 2012 - 19:19

I am relatively new to Mysql / PHP and have a database currently set up, but I think I have done it partly wrong.

I have a table containing around 1,500 entries. In these entries there is some data in 5 columns. I have been told it would be better if this info was in a new table.

I would like to move data from these 5 columns and I would also like it to copy the ID as this is important. How would I go about this?

I will give names of the table and columns if that will help.

Current table name = players

Columns = playerid, buyfrom, buydate, buydate2, buyfee, buynote.

I need to move this data from each entry into a new table called transfers. Can anyone advise??


#2 firey

    Neowinian Wise One

  • 5,356 posts
  • Joined: 30-October 05
  • Location: Ontario, Canada
  • OS: Windows 7
  • Phone: Android (4.1.2)

Posted 10 May 2012 - 19:33

I'd probably select all the data... loop through the data building an insert query out of the data for the entry into the new table.

So like.

SELECT playerid, buyfrom, buydate, buydate2, buyfee, buynote
FROM players


INSERT INTO (newTable)
(field1,2,3,4,5)
VALUES


Then do a foreach(result r in query)
split up your r (or if it's already an array) then you would append the following

(r[1],'r[2]',
r[3]',etc,etc),

then remove the last "," character. Then just run the built query.


#3 kaborka

    Neowinian²

  • 193 posts
  • Joined: 01-April 03
  • Location: Los Angeles

Posted 10 May 2012 - 19:38

In MS T-SQL, you could say

insert transfers (playerid, buyfrom, buydate, buydate2, buyfee, buynote)
select playerid, buyfrom, buydate, buydate2, buyfee, buynote from players

if PlayerID is an identity column, you must first use a Set statement to turn Identity_Insert ON for the Transfers table, and turn it off afterwards. I don't know how that's handled in MySQL

#4 OP marklcfc

    Neowinian²

  • 184 posts
  • Joined: 30-March 05

Posted 10 May 2012 - 19:42

View Postfirey, on 10 May 2012 - 19:33, said:

I'd probably select all the data... loop through the data building an insert query out of the data for the entry into the new table.

So like.

SELECT playerid, buyfrom, buydate, buydate2, buyfee, buynote
FROM players


INSERT INTO (newTable)
(field1,2,3,4,5)
VALUES


Then do a foreach(result r in query)
split up your r (or if it's already an array) then you would append the following

(r[1],'r[2]',
r[3]',etc,etc),

then remove the last "," character. Then just run the built query.


I wish I could understand that :(
I'm ok up until the VALUES part and everything that followed. :/

#5 FuhrerDarqueSyde

    Squirrelies!

  • 745 posts
  • Joined: 26-December 02
  • Location: Oshkosh, WI, USA
  • OS: Windows 7 Enterprise (Work)/Ultimate (Home)

Posted 10 May 2012 - 19:55

View Postkaborka, on 10 May 2012 - 19:38, said:

In MS T-SQL, you could say insert transfers (playerid, buyfrom, buydate, buydate2, buyfee, buynote) select playerid, buyfrom, buydate, buydate2, buyfee, buynote from players if PlayerID is an identity column, you must first use a Set statement to turn Identity_Insert ON for the Transfers table, and turn it off afterwards. I don't know how that's handled in MySQL

Indeed, <3 me some Microsoft SQL.

#6 Tjcool007

    Neowinian²

  • 103 posts
  • Joined: 27-April 11
  • Location: Belgium
  • OS: Windows 7 Pro x64, Ubuntu 13.04

Posted 10 May 2012 - 20:24

Insert ... Select could work.

INSERT INTO transfers
  SELECT playerid, buyfrom, buydate, buydate2, buyfee, buynote
  FROM players

You'll still need to create the transfers table manually first though.

#7 Wilhelmus

    Resident Elite

  • 1,302 posts
  • Joined: 19-February 05
  • Location: Finland

Posted 10 May 2012 - 20:30

If you have the 'transfers' table already created:
INSERT INTO transfers (playerid, buyfrom, buydate, buydate2, buyfee, buynote)
  SELECT playerid, buyfrom, buydate, buydate2, buyfee, buynote FROM players ORDER BY playerid;

If not:
CREATE TABLE transfers (
  SELECT playerid, buyfrom, buydate, buydate2, buyfee, buynote FROM players ORDER BY playerid
);

# add index and change playerid to auto inc.
ALTER TABLE transfers ADD PRIMARY KEY (playerid);
ALTER TABLE transfers CHANGE playerid playerid INT(11) NOT NULL AUTO_INCREMENT;