Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
How to remove left join in function Knex.JS?

How to remove left join integration?

await knex.select().from("dishes")
            .leftJoin("companies", function () {
                if (companyId) {
                    this.on("dishes.companyId", "=", "companies.id")
                } else {
                    // I want to remove left join if companyId is false
                    return false;
                }
            })

I'm got an error:

code: 'ER_PARSE_ERROR',
  sql: 'select * from `dishes` left join `companies`'
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It's unclear which part you're having difficulty with. Your current error is caused by the fact that you are trying to create a left join without giving it a condition to join on.

I suggest you move your if (companyId) check outside the leftJoin() call so that two separate queries are created.

async function getDishes(companyId) {
   if (companyId) {
      return knex.select().from("dishes").
          leftJoin("companies", "dishes.companyId", "companies.id");
   }
   return knex.select().from("dishes");
}

Which could potentially be simplified to

async function getDishes(companyId) {
   const dishes = knex.select().from("dishes");
   return companyId 
       ? dishes.leftJoin("companies", "dishes.companyId", "companies.id");
       : dishes;
 }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!