Solved

MySQL - normalize table

Posted on 2011-09-10
2
345 Views
Last Modified: 2012-05-12
How can I take current table and normalize it so that it has one keyword_tag per user_id?

Thanks in advance for your feedback.


CREATE TABLE IF NOT EXISTS `keywords_tags` (
  `user_id` int(11) default NULL,
  `updated` int(1) default '0',
  `keyword_tags` text,
  KEY `user_id` (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `keywords_tags`
--

INSERT INTO `keywords_tags` (`user_id`, `updated`, keyword_tags`) VALUES
(10, 0, '2087'),
(11, 1, '2003 2006 2018 2099'),
(12, 1, '2102 2030'),
(13, 1, '2087 2085 2089 2099 2100 2444'),
(14, 0, NULL);
0
Comment
Question by:sabecs
2 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36518188
sabecs,

This may not have the best performance in the world, but it is a method that can be used especially if this is a one time thing to fix design issue and then you will be inserting into the normalized table moving forward.

First, grab my util.numbers table script, unless you already have a numbers utility table -- in which case, substitute util.numbers for your table name.

Code to parse keyword_tags:
SELECT user_id, updated
     , SUBSTRING(`keyword_tags`, n, LOCATE(' ', CONCAT(`keyword_tags`, ' '), n)-n) `keyword_tag`
FROM `keywords_tags`
JOIN util.numbers 
   ON SUBSTRING(CONCAT(' ', `keyword_tags`), n, 2) REGEXP '^ [0-9]$'

Open in new window


Hope that helps!
0
 

Author Closing Comment

by:sabecs
ID: 36518305
Thanks mwvisa1.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question