如何在给定示例中没有任何参考的情况下获取 Sql 记录

How to get Sql Record with out any reference on given example

我有两个 table 作为 table1 和 table 2 作为

我想要一个 table3 结构作为

我没有上面两个 table 之间的任何参考。

1.Is 可以在单个 select 语句中吗?

或者

2.Required 循环播放。

我试过的是

    select E.EmpID,E.FName,E.LName,C.CityName,E.Salary,DOJ,
    case
    when E.Salary > 0 and E.Salary < 30001 then 'Trainee'
    when E.Salary > 30001 and E.Salary < 60001 then 'Jr. Developer'
    when E.Salary > 60001 and E.Salary < 150001 then 'Sr. Developer'
    when E.Salary > 150001 and E.Salary < 180001 then 'Project Lead'
    when E.Salary > 180001 and E.Salary < 250001 then 'Project Manager'
    else '' end  as Designation 
    from Emp_Master as E inner join City_Master as C
    on E.CityID = C.CityId 

我认为这是完全错误的

请回答我提供例子

您应该修正您的第一个 table,因此它包含最低和最高工资。将规则作为字符串放入 SQL 中效果不佳。

进行此更改后,您可以通过多种方式处理查询,例如相关子查询:

select t2.*,
       (select t1.designation
        from table1 t1
        where t2.salary between minsalary and maxsalary
       ) as designation
from table2 t2;

您必须将字符串转换为两个 VARCHAR 才能获得所需的 JOIN:

SELECT table2.EmpId, table2
    .FName, table2.LName, 
    table2.CityID, table2.
    DOJ, table2.salary, 
    table1.Designation
FROM table2
INNER JOIN table1 ON table2.
    salary BETWEEN CAST(LEFT(
                    table1.salary, 
                    CHARINDEX('>', 
                        table1.salary
                    ) - 1) AS INT)
        AND CAST(RIGHT(table1.
                    salary, LEN(
                        table1.salary
                    ) - CHARINDEX('>'
                        , table1.
                        salary)) AS INT
            );