Noob Question: SQL INSERT that combines static values with a SELECT statement

Hey Kysely community! I'm just getting into Kysely and I have a question about how I could run the following query with Kysely:
INSERT INTO employees (employee_id, first_name, last_name, department_id)
VALUES (1234, 'John', 'Doe',
(SELECT department_id
FROM departments
WHERE department_name = 'Sales')
);
INSERT INTO employees (employee_id, first_name, last_name, department_id)
VALUES (1234, 'John', 'Doe',
(SELECT department_id
FROM departments
WHERE department_name = 'Sales')
);
I could get an INSERT + SELECT query to work, but wasn't sure how to add static values into the mix. Thanks in advance : )
2 Replies
Kristian Notari
kysely.insertInto('employees').values({
employee_id: 1234,
first_name: 'John',
last_name: 'Doe',
department_id: eb => eb.selectFrom('departments').select('department_id').where(...)
})
kysely.insertInto('employees').values({
employee_id: 1234,
first_name: 'John',
last_name: 'Doe',
department_id: eb => eb.selectFrom('departments').select('department_id').where(...)
})
does this work? can't test it right now
AlfieGoat
AlfieGoatOP2y ago
That's exactly it, thank you!!
Want results from more Discord servers?
Add your server