Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knex.js, would like to translate "and" condition in left join

Tags:

knex.js

I have below query, I would like to represent this query in knex fromat. I have made it work by usng raw function but i am curious if it is possible with knex style.

SELECT t.id, t.title, s.userId
FROM title t LEFT JOIN
     subscribe s ON t.id  = s.titleId AND s.userId = 1;
like image 775
serkan Avatar asked Apr 10 '17 09:04

serkan


1 Answers

knex('title as t')
  .select('t.id', 't.title', 's.userId')
  .leftJoin('subscribe as s', (builder) => {
    builder.on('t.id', 's.titleId').on('s.userId', knex.raw('?', [1]));
  })
like image 85
Mikael Lepistö Avatar answered Sep 22 '22 19:09

Mikael Lepistö