スキーマの変更を検討してください。各フィールドはアトミックであると想定されています - 1 つの値を持ちます。
趣味テーブルを作成し、ユーザー テーブルを変更し、テーブルを追加して、ユーザーと趣味の間の多対多の関係を有効にします。ユーザーの趣味を示すビューを作成します。
これは、次のようなクエリを実行できることを意味します。
select * from `hobby` ; -- means your hobby check box can now be created dynamically
select * from `username_hobby` where username_id = 1 ; -- means you already have the username hobbies as separate elements.
データ正規化の詳細 => https://www.google.com.au/search?q=atomic+data+normalization
sqlfiddle の例http://sqlfiddle.com/#!2/5cdef/1/0
以下の推奨スキーマ。
DROP TABLE IF EXISTS `hobby`;
CREATE TABLE `hobby` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`hobby` varchar(31) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `hobby` (`id`, `hobby`)
VALUES
(1,'reading'),
(2,'painting'),
(3,'gaming');
DROP TABLE IF EXISTS `username`;
CREATE TABLE `username` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(31) DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `username` (`id`, `username`)
VALUES
(1,'abc');
DROP VIEW IF EXISTS `username_hobbies_view`;
CREATE TABLE `username_hobbies_view` (
`username` VARCHAR(31) DEFAULT '',
`hobby` VARCHAR(31) DEFAULT NULL,
`username_id` INT(11) UNSIGNED NOT NULL,
`hobby_id` INT(11) UNSIGNED NOT NULL
) ENGINE=MyISAM;
DROP TABLE IF EXISTS `username_hobby`;
CREATE TABLE `username_hobby` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username_id` int(11) unsigned NOT NULL,
`hobby_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `username_relationship` (`username_id`),
KEY `hobby_relationship` (`hobby_id`),
CONSTRAINT `hobby_relationship` FOREIGN KEY (`hobby_id`) REFERENCES `hobby` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `username_relationship` FOREIGN KEY (`username_id`) REFERENCES `username` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `username_hobby` (`id`, `username_id`, `hobby_id`)
VALUES
(1,1,1),
(2,1,2);
DROP TABLE `username_hobbies_view`;
CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `username_hobbies_view`
AS SELECT
`username`.`username` AS `username`,
`hobby`.`hobby` AS `hobby`,
`username_hobby`.`username_id` AS `username_id`,
`username_hobby`.`hobby_id` AS `hobby_id`
FROM ((`username` join `username_hobby` on((`username`.`id` = `username_hobby`.`username_id`))) join `hobby` on((`hobby`.`id` = `username_hobby`.`hobby_id`)));