I am trying to build the Sql Query using by using Sqlkata.I am able to build the correct sql query for one join condition ,but for And Condition i am facing issues
var empDeptQuery = new Query("employee");
empDeptQuery.Select("employee.Name", "dept.Deptname");
empDeptQuery.Join("dept", join => join.On("employee.deptid", "dept.deptid"));
SqlResult empDeptSqlKataQuery = compiler.Compile(empDeptQuery);
Final Query -empDeptSqlKataQuery.Sql is
SELECT [employee].[Name], [dept].[Deptname] FROM [employee]
INNER JOIN [dept] ON ([employee].[deptid] = [dept].[deptid])
i want to add on more condition in join clause like in the following way.
SELECT [employee].[Name], [dept].[Deptname] FROM [employee]
INNER JOIN [dept] ON ([employee].[deptid] = [dept].[deptid] And [employee].[empdeptname]=[dept].[departmentName])
i have tried this but didn't get what i expected
var empDeptQuery = new Query("employee");
empDeptQuery.Select("employee.Name", "dept.Deptname");
empDeptQuery.Join("dept", join => join.On("employee.deptid", "dept.deptid"));
empDeptQuery.Join(empDeptQuery, join => join.On("employee.empdeptname", "dept.departmentName"));
SqlResult empDeptSqlKataQuery = compiler.Compile(empDeptQuery);
Final Sql query with the above approach :
SELECT [employee].[Name], [dept].[Deptname] FROM [employee]
INNER JOIN [dept] ON ([employee].[deptid] = [dept].[deptid])
INNER JOIN (SELECT [employee].[Name], [dept].[Deptname] FROM [employee]
INNER JOIN [dept] ON ([employee].[deptid] = [dept].[deptid])) ON ([employee].[empdeptname] = [dept].[departmentName])
Please let me know how to fix the issue
Multiple joins can be described as follows; multiple join is a query that contains the same or different join types, which are used more than once. Thus, we gain the ability to combine multiple tables of data in order to overcome relational database issues.
Multiple joins can be described as a query containing joins of the same or different types used more than once, thus giving them the ability to combine multiple tables. For this article we will first create a database geeks and then create three tables in it and then run our queries on those tables. 8.
To use the WHERE clause to perform the same join as you perform using the INNER JOIN syntax, enter both the join condition and the additional selection condition in the WHERE clause. The tables to be joined are listed in the FROM clause, separated by commas. This query returns the same output as the previous example.
If you'd like to get data stored in tables joined by a compound key that's a primary key in one table and a foreign key in another table, simply use a join condition on multiple columns. In one joined table (in our example, enrollment ), we have a primary key built from two columns ( student_id and course_code ).
Use the On
or WhereX
methods as needed, for example
new Query("employee")
.Select("employee.Name", "dept.Deptname")
.Join("dept",
j => j.On("employee.deptid", "dept.deptid")
.On("employee.countryid", "dept.countryid")
)
Note that On
is just an alias for the WhereColumns
method, so all WhereX
methods works in this context also
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With