我正在尝试使用 Sqlkata 构建 Sql Query。我能够为一个连接条件构建正确的 sql 查询,但是对于 And Condition 我面临问题
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);
最终查询 -empDeptSqlKataQuery.Sql 是
SELECT [employee].[Name], [dept].[Deptname] FROM [employee]
INNER JOIN [dept] ON ([employee].[deptid] = [dept].[deptid])
我想在 join 子句中添加更多条件,如下所示。
SELECT [employee].[Name], [dept].[Deptname] FROM [employee]
INNER JOIN [dept] ON ([employee].[deptid] = [dept].[deptid] And [employee].[empdeptname]=[dept].[departmentName])
我已经尝试过了,但没有得到我所期望的
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);
使用上述方法的最终 Sql 查询:
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])
请让我知道如何解决此问题