CREATE TABLE IF NOT EXISTS `tb_research` (
`id` varchar(6) NOT NULL,
`q1` varchar(3) NOT NULL,
`q2` varchar(3) NOT NULL,
`q3` varchar(3) NOT NULL,
`q4` varchar(3) NOT NULL,
`q5` varchar(3) NOT NULL,
PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `tb_research` (`id`, `q1`, `q2`, `q3`, `q4`, `q5`) VALUES
('1', 1, '' , '' , '',2 ),
('2', '' , 1 , '', 3,1),
('3', 3,'' , '' , '',2),
('4', 2 , '' , '', '3', '5');
ตัวอย่างการนับ Code (SQL)
SELECT
id,
IF(q1>0,1,0) AS check_q1,
IF(q2>0,1,0) AS check_q2,
IF(q3>0,1,0) AS check_q3,
IF(q4>0,1,0) AS check_q4,
IF(q5>0,1,0) AS check_q5,
(IF(q1>0,1,0) + IF(q2>0,1,0) + IF(q3>0,1,0)+ IF(q4>0,1,0)+ IF(q5>0,1,0)) AS count_field
FROM `tb_research`