Forum Moderators: open

Message Too Old, No Replies

Merging 2 tables with identical columns

         

csdude55

1:08 am on Dec 21, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have 2 sets of 2 tables with identical columns, set up some 15 years ago:

alt_subjects
alt_posts

board_subjects
board_posts

There's an ID column in alt_subjects that corresponds to alt_posts, and in board_subjects that corresponds to board_posts.

I realized a long time ago that it would have been better to have alt_posts and board_posts in one table, then alt_subjects and alt_posts in a table with a column for "category" or something. But I've been procrastinating because it's a daunting task :-/

The best I can figure is to:

A. add a column to alt_subjects called "new_ID"

Then write a PHP script that will:

1. Select all of the alt_subjects and store it in an array

2. Read the last ID in board_subjects, then in a loop modify the alt_subjects array to reassigns a new ID to each row, and create an associative array to link the original ID to the new ID

3. Insert the updated alt_subjects array into board_subjects

4. Update alt_subjects with the new ID in the "new_ID" column

5. Select all of the alt_posts and store it in an array

6. Update the array with the new ID from the associative array in #2

7. Insert the update alt_posts array into board_posts

8. Modify my public script so that if the user is looking at what was once alt_subjects and the ID is less than the last ID at the time of the merge, select the new ID from alt_subjects and re-select from board_subjects

That feels like a long and tedious process! Before I jump in head first, is there an easier way?

blend27

10:31 pm on Dec 29, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



why not write a "UNION All" SQL query in a new View, not a new table.

This way you don't have to touch PHP side of it, no inserting, or updating tables, Just a View, that is what they are good for...

csdude55

8:11 pm on Jan 1, 2024 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



@blend27, that's what I do right now, but over time it's gotten a little harder to moderate. I'm about to add several more categories, so I figure that this is as good a time as any to merge them and make my life marginally easier... or infinitely harder, I guess, if I mess up while merging them! LOL

tangor

5:34 am on Jan 2, 2024 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are we talking hundreds, thousands, or millions of entries?

If view is not working for you now, you might just have to bite the bullet and do the conversions.

Just keep a full copy of all the data stored in a separate location!

csdude55

6:42 am on Jan 2, 2024 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are we talking hundreds, thousands, or millions of entries?

Combined, just under 2 million :-O

I dread the heck out of it because there's so much opportunity for error! But with having to update other tables with the new ID, I just can't think of any other way to do it than this 8-step process. This is one of those times where I wish that I had a sandbox server where I could just copy data over and test it all out first, but say la vee (haha).

not2easy

12:14 pm on Jan 2, 2024 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You can set up a local LAMP (Linux, Apache, MySQL, PHP/Perl/Python) server on a decent desktop for trial and error stuff, with several different server variable software bundles (free).

Wikipedia for the basics: https://en.wikipedia.org/wiki/LAMP_(software_bundle)

csdude55

6:07 pm on Jan 2, 2024 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I used to have Active Perl on my desktop for testing, but my server is Linux (well, CentOS) so it wasn't perfect.

I have an old laptop just sitting around, I've tossed around the idea of formatting it and installing a version of Linux on there so that I can use it as a sandbox. I should probably find the time to do that, honestly.

tangor

3:22 am on Jan 3, 2024 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Provided you have enough drive space, there are Windows install version of LAMP out there ... I use XAMP, for example.

blend27

1:57 pm on Jan 4, 2024 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Re: XAMP... OT....

On Windows Server for that matter: [microsoft.com...] << this can be extended up to 3 years, free. With Oracle Virtual Box with 5 Gigs of Ram each. I have 3 Flying right-now on a 7 year old(host converted to Ubuntu 20) laptop with 24 gb ram total. VMs are on a GIG Cat6, on 1 TB NVMe NAS. All kinds of Windows toys!