简体   繁体   English

基于是或否问题的SQL SELECT查询

[英]SQL SELECT Query based on yes or no questions

I have two question sets. 我有两个问题集。 One is Yes or No questions. 一个是是或否的问题。 Based on this question's answers I need to display another set of questions. 根据该问题的答案,我需要显示另一组问题。 For this I have added a question code for Yes or No questions. 为此,我添加了一个是或否问题的问题代码。 and for the second set questions given these question codes to identify for displaying this question or not. 对于第二组问题,给定这些问题代码以标识是否显示此问题。

For example 
Y/N questions - Question Code
1.AAAAAAAA - AAA
2.BBBBBBBB - BBB
3.CCCCCCCC - CCC

Second Set questions - Question Code
1.XXXXXXXX - AAA && BBB
2.YYYYYYYY - AAA && !BBB
3.ZZZZZZZZ - AAA && !BBB && CCC

So second set question number 1 will display only if Y/N question number 1 and 2 answer Yes. 因此,仅当是/否问题编号1和2回答是时,才会显示第二组问题编号1。 Question number 2 will display only if Y/N question number 1 answer Yes and number 2 answer No. Question number 3 will display only if Y/N question number 1 answer Yes, number 2 answer No and number 3 answer Yes. 问题编号2仅在是/否问题编号1回答是和数字2回答否时显示。问题编号3仅在是/否问题编号1回答是,数字2回答否和数字3回答是时显示。

How can we write query for displaying second set of questions to display based on the Y/N question answers. 我们如何编写查询以显示基于Y / N问题答案显示的第二组问题。

Another approach would be to normalize your tables. 另一种方法是规范化您的表。 Your 2nd table's 2nd column should really be an associative (many-to-many) table between the first and second set of questions. 第二张表的第二列实际上应该是第一组和第二组问题之间的关联表(多对多)。 With that in place your query would be much simpler. 有了这个位置,您的查询就会简单得多。

Putting this logic inside the database is in my opinion a bad design approach. 我认为将这种逻辑放入数据库中是一种不良的设计方法。 Instead I would query the results to your logic layer and analyse the results. 相反,我会将结果查询到您的逻辑层并分析结果。 From these results you could create a new query with the next questions. 从这些结果中,您可以创建带有下一个问题的新查询。

With all the logic inside your query statement it makes it very hard to maintain. 由于查询语句中包含所有逻辑,因此很难维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM