Search code examples
knex.js

Conditional SELECT in MySQL with Knex


Is there a way to add IF statements to SELECT queries using Kenx? Let's say that I want to implement the following query:

SELECT id, name, IF(grade<60,'Fail','Pass') AS examResult FROM students;

Is there a way I could do something like that with Knex without using raw?

Thanks.


Solution

  • I don't think there is any way of creating such a select statement with using knex.raw.

    Your query could look like this:

    knex.select('id', 'name', knex.raw('IF(grade<60,'Fail','Pass') AS examResult')).from('students')
    

    May I ask why you don't want to use knex.raw?