How to use select?

I have a SQL query
SELECT
row_to_json(departments.*) as department,
row_to_json(job_titles.*) as job_title,
team_member_compensations.*,
team_member_terminations.*
FROM
team_member_compensations
LEFT JOIN departments ON team_member_compensations.department_id = departments.id
LEFT JOIN job_titles ON team_member_compensations.job_title_id = job_titles.id
LEFT JOIN team_member_terminations ON team_member_terminations.team_member_compensation_id = team_member_compensations.id
WHERE ...
SELECT
row_to_json(departments.*) as department,
row_to_json(job_titles.*) as job_title,
team_member_compensations.*,
team_member_terminations.*
FROM
team_member_compensations
LEFT JOIN departments ON team_member_compensations.department_id = departments.id
LEFT JOIN job_titles ON team_member_compensations.job_title_id = job_titles.id
LEFT JOIN team_member_terminations ON team_member_terminations.team_member_compensation_id = team_member_compensations.id
WHERE ...
I am trying to translate the select but couldn't get it to work, any idea what's wrong?
db.select({
department: sql<typeof departments>`row_to_json(${departments}.*)`.as(
"department",
),
jobTitle: sql<typeof jobTitles>`row_to_json(${jobTitles}.*)`.as(
"job_title",
),
teamMemberCompensation: teamMemberCompensations,
teamMemberTermination: teamMemberTerminations,
})
.from(teamMemberCompensations)
.leftJoin(
departments,
eq(departments.id, teamMemberCompensations.departmentId),
)
.leftJoin(jobTitles, eq(jobTitles.id, teamMemberCompensations.jobTitleId))
.leftJoin(
teamMemberTerminations,
eq(
teamMemberTerminations.teamMemberCompensationId,
teamMemberCompensations.id,
),
)
.where(...)
db.select({
department: sql<typeof departments>`row_to_json(${departments}.*)`.as(
"department",
),
jobTitle: sql<typeof jobTitles>`row_to_json(${jobTitles}.*)`.as(
"job_title",
),
teamMemberCompensation: teamMemberCompensations,
teamMemberTermination: teamMemberTerminations,
})
.from(teamMemberCompensations)
.leftJoin(
departments,
eq(departments.id, teamMemberCompensations.departmentId),
)
.leftJoin(jobTitles, eq(jobTitles.id, teamMemberCompensations.jobTitleId))
.leftJoin(
teamMemberTerminations,
eq(
teamMemberTerminations.teamMemberCompensationId,
teamMemberCompensations.id,
),
)
.where(...)
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server