Solved

MySQL - normalize table

Posted on 2011-09-10
2
348 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 60

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Output in PHP throwing alignment of data off issue 12 76
mysql qry 1 38
Building a glossary into your website 9 60
MySql Linux vs Windows: bad results for Windows but why? 10 74
Creating and Managing Databases with phpMyAdmin in cPanel.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

751 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