Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Left join with multiple conditions #335

Closed
shaangidwani opened this issue Jul 11, 2023 · 7 comments
Closed

Left join with multiple conditions #335

shaangidwani opened this issue Jul 11, 2023 · 7 comments
Assignees

Comments

@shaangidwani
Copy link

Left join with multiple conditions

We need to add multiple condition in left join.

Here is the sql demo script for the same :

========================================
Table creation script

CREATE TABLE A
(
PracticeID INT
,FunctionID INT
,FunctionName VARCHAR(10)
)
GO
CREATE TABLE B
(
PatientID INT
,FunctionID INT
,FunctionValue VARCHAR(10)
)
GO
INSERT INTO A SELECT 123,1,'A'
INSERT INTO A SELECT 123,2,'B'
INSERT INTO A SELECT 123,3,'C'
INSERT INTO A SELECT 123,4,'D'
GO
INSERT INTO B SELECT 11,1,'AAA'
INSERT INTO B SELECT 11,2,'BBB'
INSERT INTO B SELECT 22,3,'CCC'
INSERT INTO B SELECT 22,4,'DDD'
GO

And output of SQL is :

----------Final Output
SELECT * FROM A
LEFT JOIN B ON B.FunctionID =A.FunctionID AND B.PatientID = 11
WHERE PracticeID = 123

You can change the B.PatientID = anything it will display the four records as left join is working fine on sql.

==================================================================

When we try to add multiple conditions on IDB studio we are unable to do this ex :

select({
from: 'Customers',
join: {
type: 'left',
with: 'Orders',
on: "Customers.customerId = Orders.customerId and Customers = 90"
}
});

And if we add in where condition then it will be converted to inner instead of left.

Kindly suggest on this asap.

@ujjwalguptaofficial
Copy link
Owner

have you tried where inside join ?

select({
    from: 'Customers',
    join: {
        type: 'left',
        with: 'Orders',
        on: "Customers.customerId = Orders.customerId",
        where: {
            customerId : 90
        }
    }
});

@shaangidwani
Copy link
Author

Yes, we have tried but it will display only one record looks like it will remove the left join and work as where condition only.

If you check SQL query it will show all the left table data including where condition data. ex : It will show all the customers who are not exits on orders and include 90. But your suggested query will only give one record which are in where condition.

Kindly suggest on this asap. Will appreciate your help.

@ujjwalguptaofficial
Copy link
Owner

aha i see - you are right. Let me see

@ujjwalguptaofficial
Copy link
Owner

The current implementation is confused b/w "&" and "where with join", let me fix it right away.

@ujjwalguptaofficial
Copy link
Owner

I have fixed in version - 4.5.7, please try and let me know. The where query inside join works for and with join.

select({
    from: 'Customers',
    join: {
        type: 'left',
        with: 'Orders',
        on: "Customers.customerId = Orders.customerId",
        where: {
            customerId : 90
        }
    }
});

This should work now.

@shaangidwani
Copy link
Author

Hey, @ujjwalguptaofficial Thanks we have an update version and it is working fine now :-)

You saved our life.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants