ejs模板引擎使用mysql,使用外键将mysql数据打印到EJS文档

I'm trying to get answers to quiz questions from mysql and print to a form using EJS templating in node/express application. I can't figure out how to print the correct answers to the corresponding question.

I would like the data to look like this in the browser:

Question 1 #id2

answer 1 #id6

answer 2 #id7

answer 3 #id8

Question 2 #id3

answer 1 #id9

answer 2 #id10

answer 3 #id11

The data is looking like this:

loadQuizes;

[ { quizId: 2,

quizName: 'Bergskedjor',

dateCreated: '2017-03-03T15:14:02.000Z',

dateFinished: '2017-03-02T23:00:00.000Z',

times: 2,

score: 20 } ]

quizQuestions;

[ { questionId: 2,

question: 'Vilket ├ñr v├ñrldens h├Âgsta berg?',

questionQuizid: 2 },

{ questionId: 3,

question: 'Vilket ├ñr v├ñrldens tredje h├Âgsta berg?',

questionQuizid: 2 } ]

answers;

[ { answerId: 6,

answer: 'Question 1 Answer 1',

correct: 0,

answerQuestionid: 2 },

{ answerId: 7,

answer: 'Question 1 Answer 2',

correct: 1,

answerQuestionid: 2 },

{ answerId: 8,

answer: 'Question 1 Answer 3',

correct: 0,

answerQuestionid: 2 },

{ answerId: 9,

answer: 'Question 2 Answer 1',

correct: 0,

answerQuestionid: 3 },

{ answerId: 10,

answer: 'Question 2 Answer 2',

correct: 0,

answerQuestionid: 3 },

{ answerId: 11,

answer: 'Question 2 Answer 3',

correct: 1,

answerQuestionid: 3 } ]

Arriving from tables:

mysql> SELECT * FROM quiz;

+--------+------------------------+---------------------+--------------+-------+-------+

| quizId | quizName | dateCreated | dateFinished | times | score |

+--------+------------------------+---------------------+--------------+-------+-------+

| 1 | Solution to everything | 2017-03-03 16:14:02 | 2017-03-03 | 2 | 20 |

| 2 | Bergskedjor | 2017-03-03 16:14:02 | 2017-03-03 | 2 | 20 |

+--------+------------------------+---------------------+--------------+-------+-------+

mysql> SELECT * FROM question;

+------------+-------------------------------------------+----------------+

| questionId | question | questionQuizid |

+------------+-------------------------------------------+----------------+

| 1 | What color is the Sky? | 1 |

| 2 | Vilket ├ñr v├ñrldens h├Âgsta berg? | 2 |

| 3 | Vilket ├ñr v├ñrldens tredje h├Âgsta berg? | 2 |

+------------+-------------------------------------------+----------------+

mysql> SELECT * FROM answers;

+----------+---------------------+---------+------------------+

| answerId | answer | correct | answerQuestionid |

+----------+---------------------+---------+------------------+

| 1 | Red | 0 | 1 |

| 2 | Green | 0 | 1 |

| 3 | Blue | 1 | 1 |

| 4 | Pink | 0 | 1 |

| 5 | Red | 0 | 1 |

| 6 | Question 1 Answer 1 | 0 | 2 |

| 7 | Question 1 Answer 2 | 1 | 2 |

| 8 | Question 1 Answer 3 | 0 | 2 |

| 9 | Question 2 Answer 1 | 0 | 3 |

| 10 | Question 2 Answer 2 | 0 | 3 |

| 11 | Question 2 Answer 3 | 1 | 3 |

+----------+---------------------+---------+------------------+

This is my solutions so far:

But that gives me:

Question 1 #id2

- answer 1 #id6

- answer 2 #id7

- answer 3 #id8

- answer 1 #id9

- answer 2 #id10

- answer 3 #id11

Question 2 #id3

- answer 1 #id6

- answer 2 #id7

- answer 3 #id8

- answer 1 #id9

- answer 2 #id10

- answer 3 #id11

解决方案

As far as I can tell, you're selecting everything from the answers table that satisfy both questions:

SELECT * FROM answers WHERE answerQuestionid IN (SELECT questionId FROM question WHERE questionQuizid = ?);

You probably should try adding a condition in your view logic:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值