• 0

SQL query required for moving data


Question

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??

Link to comment
https://www.neowin.net/forum/topic/1076463-sql-query-required-for-moving-data/
Share on other sites

6 answers to this question

Recommended Posts

  • 0

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.

  • 0

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

  • Like 1
  • 0

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. :/

  • 0
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.

  • 0

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;
[/CODE]

If not:
[code]
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;

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Absolutely 👍
    • Exactly what I was thinking. All of a sudden in span of a month multiple CEO's from scam altman to this clown has had sudden change of heart does not seem organic change lol
    • Microsoft releases Visual Studio Code 1.124 with smarter autonomous AI agents by Paul Hill Microsoft has just released Visual Studio Code 1.124 with a focus on faster agent workflows and improved agent autonomy. Microsoft outlined the following features as the key items in this update: Autopilot: Autopilot, enabled by default, is now smarter to determine when a task is truly done. Background sessions: Quickly send a request in the background and keep composing the next session. Session navigation: Search, jump, and step through agent sessions with the keyboard. Browser history: Revisit and search pages you've already opened in the integrated browser. With VS Code 1.124, Microsoft has enabled Autopilot by default. For those that don’t know, Autopilot is a chat permission level that you can pick to give agents permission to take initiative and act autonomously, without needing explicit user approval for each action. Also related to Autopilot, Microsoft introduced Advanced Autopilot, which changes how Autopilot decides when to keep iterating and when to finish. This helps you get more complete results without manually monitoring loops. This feature works using a small utility model that reads a transcript of the chat and decides when the task is done. Another new feature in 1.124 is the Agents window, which lets you easily explore, iterate on, and review agent sessions across projects and machines. Previously, starting a new agent session meant waiting for it to load before you could compose the next one. With this update, sessions can be requested in the background. This VS Code update also brings session navigation updates to switch between them more quickly. The update also lets you reload or reopen the Agents window so that it no longer loses your layout, so you will land back where you left off. If you use the integrated browser in VS Code, you will notice that it now retains the history of visited pages. Suggestions will now show when typing in the URL bar and can be managed by using Ctrl+H within a browser tab. The browser now also lets you customize the toolbar more; just right-click on the toolbar area to the right of the URL input. Finally, the browser has faster agentic text entry. Another improvement is experimental enterprise-managed Copilot plugin policies that allow admins to centrally control which chat plugins and plugin marketplaces are available to developers. If you have VS Code installed, 1.124 should install automatically, or you'll get a prompt. If you don't have it installed, get it here.
  • Recent Achievements

    • First Post
      X-No-file earned a badge
      First Post
    • One Month Later
      johnjacobb40 earned a badge
      One Month Later
    • One Year In
      Primer1st earned a badge
      One Year In
    • Experienced
      JayZJay went up a rank
      Experienced
    • Reacting Well
      Sir_Timbit earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      514
    2. 2
      PsYcHoKiLLa
      219
    3. 3
      +Edouard
      145
    4. 4
      Steven P.
      86
    5. 5
      ATLien_0
      86
  • Tell a friend

    Love Neowin? Tell a friend!