我正在建立一个网站,其中包含用户填写的问卷。目前我的数据库看起来如下所示。
网站问卷由
组成问题
这应该放在我的数据库中吗?我完全失去了这个?
当前数据库设计 请参阅小提琴http://sqlfiddle.com/#!6/bf068/1
User_Table
ID | UserName
0 | Jack
...
User Questionnaire_Questions_Answer
ID | user_id | question_id | answer_id
0 | 0 | 0 | 0
1 | 0 | 1 | 3
...
Questionnaire_Questions
ID | Question
0 | What type of music do you like?
1 | What is your favorite sport ?
...
Questionnaire_Answers
ID | Answer
0 | Rock
1 | Rap
2 | Basketball
3 | Soccer
...
搜索SQL语句
根据用户正在寻找的偏好搜索最佳问卷调查结果,按最高总匹配数排序
SELECT
User_Table.id,
User_Table.UserName,
COUNT(User_Table.id) as totalMatches
FROM User_Table
INNER JOIN Questionnaire_Questions_Answer ON User_Table.id = Questionnaire_Questions_Answer.user_ID
INNER JOIN Questionnaire_Questions ON Questionnaire_Questions.id = Questionnaire_Questions_Answer.question_ID
INNER JOIN Questionnaire_Answers on Questionnaire_Answers.id = Questionnaire_Questions_Answer.answer_ID
WHERE
--Q and A Requested to Match
Questionnaire_Questions.id = '0' and Questionnaire_Answers.answer = '0'
OR
Questionnaire_Questions.id = '1' and Questionnaire_Answers.answer = '3'
GROUP BY User_Table.id
ORDER BY totalMatches DESC
示例结果
ID | Name | totalMatches
0 | Jack | 2
答案 0 :(得分:0)
创建一个位置表,如下所示:
create table dbo.location (id int identity,
country varchar(200),
state_province varchar(100),
city varchar 100))
添加用户时,将该位置的ID添加到用户表。