Knex onIn 用于列
Knex onIn for Columns
我需要使用 Knex 达到以下 SQL 结果:
select themes.id as theme_id,
themes.title as theme_title,
subthemes.id as subtheme_id,
subthemes.title as subtheme_title,
subthemes.parent_id as subtheme_parent_id
from sections as themes
left join sections as subthemes on subthemes.parent_id = themes.id
left join section_questions on section_questions.section_id in (themes.id, subthemes.id)
where subthemes.parent_id is not null
上面是一个简单的自连接,但是我使用Knex失败的地方是在使用以下语句时:
.leftJoin<SectionQuestion>('section_questions', (join) =>
join.onIn('section_questions.section_id', ['themes.id', 'subthemes.id'])
)
至少根据 Knex API,以上列名被视为值。
列的等效项是什么?
在这种情况下,您可以使用 knex.raw
使其成为列名。
.leftJoin<SectionQuestion>('section_questions', (join) =>
join.onIn('section_questions.section_id', [
knex.raw('??', 'themes.id'),
knex.raw('??', 'subthemes.id'),
])
);
我需要使用 Knex 达到以下 SQL 结果:
select themes.id as theme_id,
themes.title as theme_title,
subthemes.id as subtheme_id,
subthemes.title as subtheme_title,
subthemes.parent_id as subtheme_parent_id
from sections as themes
left join sections as subthemes on subthemes.parent_id = themes.id
left join section_questions on section_questions.section_id in (themes.id, subthemes.id)
where subthemes.parent_id is not null
上面是一个简单的自连接,但是我使用Knex失败的地方是在使用以下语句时:
.leftJoin<SectionQuestion>('section_questions', (join) =>
join.onIn('section_questions.section_id', ['themes.id', 'subthemes.id'])
)
至少根据 Knex API,以上列名被视为值。
列的等效项是什么?
在这种情况下,您可以使用 knex.raw
使其成为列名。
.leftJoin<SectionQuestion>('section_questions', (join) =>
join.onIn('section_questions.section_id', [
knex.raw('??', 'themes.id'),
knex.raw('??', 'subthemes.id'),
])
);