question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

LEFT OUTER JOIN with many criterias. One of the criterias is comparing with a string.

See original GitHub issue

I need to write a MySQL query like this:

SELECT *
FROM student LEFT OUTER JOIN student_languages
              ON student.id = student_languages.student_id
              AND student_languages.code = 'en_US'

I achieved it with the following code:

Knex('student')
.join('student_languages', ->
    @type('left outer')
    .on(    'student.id',           '=',    'student_languages.student_id')
    .andOn( 'student_languages.code',   '=',    Knex.raw('\'' + languageCode + '\''))
)
.select()

Knex.raw(‘'’ + languageCode + ‘'’)) allow the languageCode to be recognized as a string.

AND `student_languages`.`code` = 'en_US'

If I obmit Knex.raw, string value of languageCode is understood as a column name which is not what I need.

AND `student_languages`.`code` = `en_US`

However, the problem is with the usage of Knex.raw which leaves potential SQL injection.

Is there a non SQL-injection exposure way to achieve what I need?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
boutellcommented, Feb 18, 2017

Is this still the only way to do a simple equality test with something like a string in an “on” clause? I see I can do “onIn”, so that’s a workaround, but surely I should also be able to compare with a simple value without using “in”?

0reactions
boutellcommented, Feb 18, 2017

Perhaps onScalar, since I understand it would not be practical to detect the difference between .on with a column name and .on with a string.

Read more comments on GitHub >

github_iconTop Results From Across the Web

LEFT OUTER JOIN with many criterias. One of the ... - GitHub
I need to write a MySQL query like this: SELECT * FROM student LEFT OUTER JOIN student_languages ON student.id ...
Read more >
SQL Understanding Outer Joins with Criteria and Why the ...
Question 1 - Outer Joins with Criteria​​ The way to get around this is to include an "OR IS NULL" in the second...
Read more >
LEFT OUTER JOIN on multiple conditions
The difference is that in this case two nulls are considered to be equal to each other. (When comparing them using = ,...
Read more >
Left and Right Joins Using the Plus (+) Sign in Oracle - Chartio
An INNER JOIN in a relational database is simply the joining of two or more tables in which the result will only contain...
Read more >
SQL Equi join - w3resource
SQL EQUI JOIN performs a JOIN against equality or matching column(s) values of the associated tables. An equal sign (=) is used as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found