Mysql bir alanda birden çok dinamik değerleri saklamak

2 Cevap php

Nasıl bir MySQL veritabanı tek bir alanda birden çok değer (sayı ve kelime) depolamak ve daha sonra onları tekrar ayıklama hakkında gitmek ve gerektiğinde onları MySQL ve PHP kullanarak yapabilirsiniz?

for example: I want to store the dynamic values a user will enter using a form for example 1, 2, foo, tree, and monkey all in the same field in a database.

then i want to extract it and put them on seperate lines for example: 1 2 foo tree monkey

herhangi bir fikir?

2 Cevap

Sen bir diziye bütün değerleri koymak ve sonra serialize:

$string = serialize(array(1, 2, 'foo', 'tree', 'monkey');

Bu size veritabanında depolamak bir dize verecektir. Daha sonra, bunu de-serializing ile dizi kurtarabilirsiniz:

$array = unserialize($string);

Eğer malzeme bir bütün takım işleyebilir bir veri türü bahsediyorsan eğer, text, aksi takdirde bu kötü bir fikir olduğunu ve bu not nasıl veri depolama gerektiğidir kullanabilirsiniz normalleştirilmiş ilişkisel veritabanı. Eğer saklıyorsanız ne bilgi verir misiniz?

Herhangi bir guru daha iyi bir şema stratejisi varsa, bu ben ile geldi budur .. bana bildirin böylece kendimi bir SQL noob ben:

Dump:

/*

Navicat MySQL Data Transfer


Date: 2009-10-20 03:01:18

*/



SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------

-- Table structure for `job_scores`

-- ----------------------------

DROP TABLE IF EXISTS `job_scores`;

CREATE TABLE `job_scores` (

  `job_id` int(2) NOT NULL,

  `user_id` int(2) NOT NULL,

  `rating` tinyint(2) NOT NULL

) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;



-- ----------------------------

-- Records of job_scores

-- ----------------------------

INSERT INTO `job_scores` VALUES ('1', '1', '10');



-- ----------------------------

-- Table structure for `jobs`

-- ----------------------------

DROP TABLE IF EXISTS `jobs`;

CREATE TABLE `jobs` (

  `id` int(2) NOT NULL auto_increment,

  `name` varchar(50) collate utf8_unicode_ci default NULL,

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;



-- ----------------------------

-- Records of jobs

-- ----------------------------

INSERT INTO `jobs` VALUES ('1', 'plumber');



-- ----------------------------

-- Table structure for `users`

-- ----------------------------

DROP TABLE IF EXISTS `users`;

CREATE TABLE `users` (

  `id` int(2) NOT NULL auto_increment,

  `name` varchar(50) collate utf8_unicode_ci NOT NULL,

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;



-- ----------------------------

-- Records of users

-- ----------------------------

INSERT INTO `users` VALUES ('1', 'John');

Örnek sorgu:

SELECT

jobs.name as job_name, users.name as user_name, job_scores.rating

FROM

job_scores


INNER JOIN jobs ON jobs.id = job_scores.job_id
INNER JOIN users on users.id = job_scores.user_id

WHERE 

user_id = 1

Sonuç:

tesisatçı John 10