Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Create new table from 1 existing table column
Old 07-26-2008, 05:40 PM Create new table from 1 existing table column
Galaxian's Avatar
Rich Powell

Posts: 842
Name: Rich Powell
Location: United Kingdom
Trades: 0
I'm trying to create a categories table seperate from the main content table.

The problem right now is that all the categories are in names in the current table.

So

ID Category Title
1 Categoryname Title1
2 Categoryname Title2
3 Categoryname2 Title3

The category column will be moved to a seperate table:

ID Category_Name
1 Categoryname
2 Categoryname2
etc

However the content table is huge with over 20,000 records so I'm presuming that a while loop through each record would take too long. How'd I go about doing this?
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Please help get the new
Please login or register to view this content. Registration is FREE
forum started for Webmasters like you!

Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
 
Register now for full access!
Old 07-26-2008, 10:49 PM Re: Create new table from 1 existing table column
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I duplicated your tables with this:
Code:
CREATE TABLE category (
  category_id int(10) unsigned NOT NULL auto_increment,
  `name` varchar(25) NOT NULL,
  PRIMARY KEY  (category_id)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

INSERT INTO category (category_id, name) VALUES 
(1, 'categoryname'),
(2, 'categoryname2');

CREATE TABLE content (
  content_id int(10) unsigned NOT NULL auto_increment,
  category varchar(25) NOT NULL,
  title varchar(25) NOT NULL,
  PRIMARY KEY  (content_id)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8;

INSERT INTO content (content_id, category, title) VALUES 
(1, 'categoryname', 'title 1'),
(2, 'categoryname2', 'title 2'),
(3, 'categoryname', 'title 3');
I then executed this query:

Code:
UPDATE content c1 SET c1.category=(SELECT c2.category_id FROM category c2 WHERE c2.name=c1.category)
And then changed the field type of category:
Code:
ALTER  TABLE  `content`  CHANGE  `category`  `category` INT  UNSIGNED NOT  NULL
Now, I didn't watch my field types very closely. My main goal was to demonstrate how, so be sure to make adjustments based on your field types.

With a lot of records, you may want to run the change on an external server and then import all the changes.

Hope that helps!
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Create new table from 1 existing table column
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.09963 seconds with 12 queries