我正在尝试在leftJoin中进行区分,但是.distinct()
似乎不适用于我的上下文。
我有这个查询:
const questions = Database
.query()
.select('book_unit_question.*')
.select('book_unit.unit')
.select('book_unit.sequence')
.select('books.id as book_id')
.select('book_unit_question_option.description as correct_answer_description_id')
.select('users.username')
.from('book_unit_question')
.leftJoin('book_unit_question_option', 'book_unit_question.id', 'book_unit_question_option.book_unit_question_id')
.distinct('book_unit_question.id')
.innerJoin('book_unit', 'book_unit.id', 'book_unit_question.book_unit_id')
.innerJoin('books', 'books.id', 'book_unit.book_id')
.innerJoin('users', 'book_unit_question.user_id', 'users.id')
.where('books.id', '=', request.params.id)
但是当我得到结果时,我已经重复book_unit_question.id
了:
0: {id: 11, book_unit_id: 2, question_form: "Texto", option_form: "Texto",…}
1: {id: 11, book_unit_id: 2, question_form: "Texto", option_form: "Texto",…}
2: {id: 11, book_unit_id: 2, question_form: "Texto", option_form: "Texto",…}
3: {id: 12, book_unit_id: 2, question_form: "Texto", option_form: "Texto", type_answer: "Aberta",…}
我只需要:
0: {id: 11, book_unit_id: 2, question_form: "Texto", option_form: "Texto",…}
1: {id: 12, book_unit_id: 2, question_form: "Texto", option_form: "Texto", type_answer: "Aberta",…}