sql查询 - 从2个表中选择记录而不加入它们

时间:2011-09-28 16:50:59

标签: sql tsql

我有两张表如下:

emp_id emp_name  emp_add  emp_no dept_name
1      sss       hhh      0      hhh
2      wsss      ddd      0      hhh

2nd table is as follows:
dep_name dept_no
hhh      1

我只从table 1中选择记录,其中dept_name与第二个表匹配。 我不能使用连接,因为表中有300条记录与表1记录匹配。 我还要将emp_notable 1的值设置为dept_no的{​​{1}}。
有什么帮助吗?

2 个答案:

答案 0 :(得分:6)

我认为没有理由避免使用连接。

UPDATE t1
    SET emp_no = t2.dept_no
    FROM table1 t1
        INNER JOIN table2 t2
            ON t1.dept_name = t2.dept_name

答案 1 :(得分:-2)

试试这个

select t1.*, (select top 1 dept_name from table2 t2 where t2.dept_no = t1.dept_no)
from table1 t1
相关问题