加入2个表VB.NET

时间:2018-12-12 07:06:16

标签: sql-server vb.net

我得到了以下代码:

strSQL += " ,allow_grp_desc as PayAlwGroup, deduct_grp_desc as PayDedGroup, unit_rate_grp_desc as PayUnitRateGroup, Ot_grp_name as OTGrpName

这将分别生成具有不同列的报告。所有列字段都在同一表(员工表)中。因此,我想在报告中添加新的列名“ is_prorate”,但它属于另一个名为“ employee_salary”的表。有人可以建议我如何插入编码,因为我已经尝试了许多解决方案,但是没有用。

示例:

strSQL += " SELECT CASE is_basic_prorate WHEN '1' THEN 'Y' ELSE 'N' as BasicProrate from employee_salary_setup WHERE Company_id = '" & gstrCompanyId & "' ,"

错误消息:

  

关键字“ INNER”附近的语法不正确。附近的语法不正确   关键字“ as”。

预期输出:

PayAlwGroup PayDedGroup PayUnitRateGroup    OTGrpName        BasicProrate
STANDARD    STANDARD    STANDARD            NO OVERTIME          Y

2 个答案:

答案 0 :(得分:0)

您可以如下所示扩展查询

strSQL += " SELECT is_prorate, CASE is_basic_prorate WHEN '1' THEN 'Y' ELSE 'N' as BasicProrate from employee_salary_setup inner join employee_salary on employee_salary_setup.EmpId = employee_salary.EmpId  WHERE Company_id = '" & gstrCompanyId & "' ,"

要了解有关加入sql server的更多信息,您可以学习SQL Server Joins

答案 1 :(得分:0)

我终于得到了答案。让我分享您的解决方案。

strSQL += " ,allow_grp_desc as PayAlwGroup, deduct_grp_desc as PayDedGroup, unit_rate_grp_desc as PayUnitRateGroup, Ot_grp_name as OTGrpName, employee_salary_setup.CASE is_basic_prorate WHEN '1' THEN 'Y' ELSE 'N' as BasicProrate

strSQL += " from employee LEFT OUTER JOIN employee_salary_setup ON employee.emp_id = employee_salary_setup.emp_id"

结果与问题中的预期输出相同。