我想将员工体验数据存储在数据库中,员工可以一次添加多种体验。
数组:
Array
(
[start_date] => Array
(
[0] => 2016-07-12
[1] => 2016-09-16
)
[end_date] => Array
(
[0] => 2016-09-09
[1] => 2017-01-20
)
[total_month] => Array
(
[0] => 2
[1] => 4
)
[firm_name] => Array
(
[0] => sadsadasd
[1] => 34343
)
[turnover] => Array
(
[0] => 343443434
[1] => 443434
)
[student_experiece] => Array
(
[0] => 343434
[1] => 343434
)
[orderof_supply_food] => Array
(
[0] => 343443
[1] => 434343434
[2] => 34334
[3] => 34343
)
[payment_against_bill] => Array
(
[0] => 3434343
[1] => 434343
)
[vat] => Array
(
[0] => 3434343
[1] => 3434343434
)
)
我所做的是将数组转换为字符串,然后存储在数据库中,如下所示:
[start_date] = 2016-07-12,2016-09-16,
[end_date] =2016-09-09,2016-01-20
数据库结构:
答案 0 :(得分:1)
在数据库中存储以逗号分隔的值通常会违反First Normal Form。有关详细信息,请参阅this answer。
我建议使用两个表:一个用于员工,一个用于体验。然后,您可以在"体验"中使用外键。表引用"雇员"中的主键。表
为了示例,我简化了您的表格结构:
CREATE TABLE IF NOT EXISTS `employees` (
`id` mediumint(8) UNSIGNED NOT NULL AUTO_INCREMENT,
`name_first` varchar(50) NOT NULL,
`name_last` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `experiences` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`employee_id` int(11) NOT NULL,
`date` date NOT NULL,
`description` text NOT NULL,
PRIMARY KEY (`id`),
KEY `employee_id` (`employee_id`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
INSERT INTO `employees` (`id`, `name_first`, `name_last`) VALUES
(1, 'Jane', 'Doe'),
(2, 'John', 'Doe'),
(3, 'Elmer', 'Fudd'),
(4, 'Tester', 'McTesterson'),
(5, 'Sally', 'Jones');
INSERT INTO `experiences` (`id`, `employee_id`, `date`, `description`) VALUES
(1, 2, '2016-07-01', 'Amazing Experience'),
(2, 4, '2016-07-02', 'Testing'),
(3, 2, '2016-07-03', 'Another Experience'),
(4, 3, '2016-07-05', 'Test Experience'),
(5, 1, '2016-07-07', 'Sample Experience'),
(6, 2, '2016-07-15', 'An experience');
请注意,experiences
表有一个名为" employee_id"的外键。此值与employees
表中员工的主键相关。
使用此结构,您无需为每次体验重复员工数据。可以轻松添加新体验并与现有员工相关联。您可以使用JOIN
获取关联数据,具体取决于您所需的输出。
按日期升序获取所有体验:
SELECT emp.`name_first`,emp.`name_last`,exp.`date`,exp.`description`
FROM `experiences` exp
LEFT JOIN `employees` emp ON (emp.`id`=exp.`employee_id`)
WHERE 1
ORDER BY exp.`date` ASC;
name_first name_last date description
John Doe 2016-07-01 Amazing Experience
Tester McTesterson 2016-07-02 Testing
John Doe 2016-07-03 Another Experience
Elmer Fudd 2016-07-05 Test Experience
Jane Doe 2016-07-07 Sample Experience
John Doe 2016-07-15 An experience
计算每位员工的经验:
SELECT emp.`name_last`,emp.`name_first`,COUNT(exp.`id`) as `experience_count`
FROM `employees` emp
LEFT JOIN `experiences` exp ON (exp.`employee_id`=emp.`id`)
GROUP BY emp.`id`
ORDER BY emp.`name_last` ASC;
name_last name_first experience_count
Doe John 3
Doe Jane 1
Fudd Elmer 1
Jones Sally 0
McTesterson Tester 1
以下是您可能会发现有用的信息资源:
Database Normalization - Explained with Examples
Normalization in Database (With Example)
Normalization of Database
答案 1 :(得分:0)
您可以使用json_encode()
将PHP-Array编码为JSON-String。当您从数据库中获取JSON-String时,可以使用json_decode()
将其解码为PHP数组。然后,您可以修改它并将其存储回数据库。