A user is required to complete a questionnaire – answering the questions in various categories.
Depending on the responses from the user, the user will be presented with a number of recommendations.
- A recommendation could have a 1:1 relation with the question asked.
- A recommendation could be made based on either one, or another question being answered. (Either, Or)
- Some recommendations will be made based upon responses from say 3 of 9 questions. e.g. recommendation 1 will be shown when 3 from a set of questions are answered positively.
I am looking for assistance with somehow creating a generic lookup so that based on the user’s responses, I can make the recommendations.
Questions may be added and removed, similarly, recommendations may be added and removed. I need to keep the system quite generic and extensible.
Schema so far:
CREATE TABLE `categories` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`category` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`order` int(11) NOT NULL DEFAULT '10',
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `questions` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`category_id` int(10) unsigned NOT NULL,
`question` text COLLATE utf8_unicode_ci NOT NULL,
`order` int(11) NOT NULL DEFAULT '10',
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=35 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE `recommendations` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`recommendation` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`code` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
1
What you seem to need is an additional table mapping questions to a question group and another mapping question groups to recommendations. Based on that data, you could query the questionnaire and find all question groups that it completely matches. From this, you can derive your recommendations.
Most people in production are painfully forced to keep things simple as the data size grows. There is normal form and non normal form in design and real world is a different animal. So you may not want a five table join in production.
So in non normal form, one table. It is important to isolate the data changes so you are not forced to change the structure of the tables in production because of client functionality shift. Can use XML, HTML or other format like JSON. The goal here is to keep the structure simple and yet flexible as client needs are always changing.
A good check is to always write out your select statements to match your business functionality and simplify your tables accordingly. Business rules in the master design will clarify the relationships.
Without a full context a complete answer is futile with your question.
1