如何检查sql server中的条件?

时间:2010-10-21 19:26:21

标签: sql sql-server

表1

   slitem    firstname    lastname
    1         aaa           bbb

表2

slitem    firstname    lastname
 1         null           null
 2          null        null

结果:

slitem    firstname    lastname
    1         aaa           bbb
    2        null          null

我想加入这两张桌子......

帮帮我

3 个答案:

答案 0 :(得分:2)

SELECT COALESCE(t1.slitem, t2.slitem) AS slitem, 
       COALESCE(t1.firstname, t2.firstname) AS firstname, 
       COALESCE(t1.lastname, t2.lastname) AS lastname
FROM      table1 t1 
FULL JOIN table2 t2 ON t1.slitem = t2.slitem

(编辑,因为OMG小马将我的FULL JOIN编辑成LEFT JOIN ......这不是我的意图!)

答案 1 :(得分:0)

听起来你正在寻找一个左外连接:

SELECT * FROM Table1 LEFT JOIN Table2 ON table2.slitem = Table1.slitem WHERE 1 = 1

答案 2 :(得分:0)

你想要一个左连接,就像其他答案所说的那样......但是考虑到这是一个相当基本的事情,你知道在使用SQL时,你可能想要读一点。 Here's a good place to start.