• 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

    • Price Drop: Save 90% on ChatPlayground AI lifetime plan, and compare multiple AI models by Steven Parker Today's highlighted deal comes via our Apps + Software section of the Neowin Deals store, where for only a limited time, you can save up to 90% on ChatPlayground AI: Lifetime Subscriptions. ChatPlayground AI puts the world’s top AI models in one powerful interface, letting you enter a single prompt and instantly compare outputs from multiple models to choose the perfect response for your needs. Boost productivity and creativity with access to the latest AI giants like GPT-4o, Claude Sonnet 4, Gemini 1.5 Flash, DeepSeek V3, and dozens more — all in one window. Whether you’re chatting, coding, generating images, or refining prompts, ChatPlayground AI equips you with advanced tools like prompt engineering, image/PDF chat, saved conversations, and AI image creation, plus priority support to keep your workflow seamless. Access the world’s best AI models Side-by-Side Comparisons: Enter one prompt & instantly view results from multiple AI models to find the best output for your needs 40+ AI Models: Includes GPT-4o, Claude Sonnet 4, Gemini 1.5 Flash, DeepSeek V3, Llama, Perplexity, and many more Multi-Function Platform: Access AI for chat, image generation & coding all within a single interface Web Browser Extension: Offers a Chrome extension to seamlessly integrate the platform into your browsing workflow Boost productivity with powerful features ChatPlayground Interface: Designed for seamless AI model comparison in one window Prompt Engineering: Refine & optimize your prompts for better, more accurate responses Chat with Images & PDFs: Upload visuals and documents to get context-aware answers Saved Chat History: Keep track of past conversations for reference & ongoing projects AI Image Generation: Create high-quality visuals powered by top AI image models Priority Customer Support: Get faster assistance whenever you need it What you'll get with the Unlimited Plan Includes unlimited messages/month Built for prompt engineers, startups, and teams who run experiments nonstop Includes priority access to new features and future models Good to know Length of access: lifetime Redemption deadline: redeem your code within 30 days of purchase Access options: Desktop Max number of device(s): Unlimited Available to both NEW & Existing users Updates included A lifetime subscription to ChatPlayground AI (Unlimited Plan) normally costs $619, but you can pick it up for just $59.97 for a limited time - that represents a saving of $614 (90% off). Click the link below for more details, always check terms and specifications before making a purchase. Get this ChatPlayground AI (Unlimited) for $59.97 (was $619) There are also two other discounted plans to choose from. Although priced in U.S. dollars, this deal is available for digital purchase worldwide. Support queries If you have queries or need support for any of the Neowin Deals, please use the contact form here. Neowin Deals are managed and sold by StackCommerce who represent Neowin on an affiliate basis. Why we post these deals We post these because we earn commission on each sale so as not to rely solely on advertising, which many of our readers block. It all helps toward paying staff reporters, servers and hosting costs. So for those that keep moaning and complaining, be thankful we're still online for you to even do that. Other ways to support Neowin Whitelist Neowin by not blocking our ads Create a free member account to see fewer ads Make a donation to support our day to day running costs Subscribe to Neowin - for $14 a year, or $28 a year for an ad-free experience Disclosure: Neowin benefits from revenue of each sale made through our branded deals site powered by StackCommerce.
    • Amazon Deal: These AMD RX 9070 XT GPUs are very good offers by Sayan Sen Following the release of AMD's new RX 9070 GRE last week for $549 (our review), AMD currently has a couple of great deals on the more powerful 9070 XT wherein the GPU is on sale for $650. We already covered the Gigabyte Gaming model a couple of days ago which is still live at that price. Aside from that, the white ICE variant is also at the same price (purchase link under the specs table down below). This is interesting to note considering white cards are generally more expensive as they come with a premium attached. Similar to the Gaming model, the ICE card too employs Gigabyte's WINDFORCE cooling system which combines multiple design elements such as alternate-spinning Hawk fans, a vapor chamber, and composite heat pipes to manage heat dissipation. The Server-grade Thermal conductive gel further helps in this department. The inclusion of a semi-passive cooling mode allows the fans to remain inactive under low workloads, which allows for reduced noise operation during lighter usage. There is dual BIOS switch that allows toggling between performance and silent modes. The tech specs of the card are given in the table below: Specification Value Boost Clock Up to 3060 MHz (Reference Card: 2970 MHz) Game Clock Up to 2520 MHz (Reference Card: 2400 MHz) Stream Processors 4096 Memory Size 16 GB Memory Type GDDR6 Memory Clock 20 Gbps Memory Bus Width 256-bit PCI Express Interface PCI Express 5.0 Max Resolution 7680 × 4320 Maximum Displays Supported 4 Display Outputs 2 × DisplayPort 2.1a 2 × HDMI 2.1b Card Dimensions 288 × 132 × 56 mm (L × W × H) (2.7 slot) Recommended Power Supply 750 W Power Connectors 3 × 8-pin PCIe Get it at the link below: GIGABYTE Radeon™ RX 9070 XT Gaming OC ICE 16G Graphics Card (GV-R907XGAMINGOCICE-16GD): $649.99 (Sold and Shipped by Amazon US) (Was: $750) If your budget is lower, check this Nvidia RTX 5060 Ti 8GB deal out for only $330 wherein you get the latest James Bond game free. This Amazon deal is US-specific and not available in other regions unless specified. This is a first-party seller link (at the time of article publishing); ensure that you also purchase from a first-party seller link only. If you don't like it or want to look at more options, check out the previous deals that we have covered, OR you can also visit Amazon US deals page. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • What about it? The old MV2 version will not work.
  • Recent Achievements

    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
    • One Year In
      jojodbn earned a badge
      One Year In
    • One Month Later
      jojodbn earned a badge
      One Month Later
    • Week One Done
      jojodbn earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      516
    2. 2
      PsYcHoKiLLa
      229
    3. 3
      +Edouard
      118
    4. 4
      ATLien_0
      87
    5. 5
      Steven P.
      83
  • Tell a friend

    Love Neowin? Tell a friend!