LEFT OUTER JOIN with many criterias. One of the criterias is comparing with a string.
See original GitHub issueI 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:
- Created 10 years ago
- Comments:8 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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”?
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.