日期:2014-05-19  浏览次数:20406 次

求一语句或存储过程!
我有两张表:
table1:                           table2:            
col1   col2                           col1           col2  
a           1                                 a                   x
b           2                                 b                   y
c           3                                 f                   m
d           1                                 g                   a  
e           3                                 h                   m

想通过查询将量表关连起来变成如下结果:
table1
col1       col2     col3
a               1           x
b               2           y
c               3           null
d               1           null
e               3           null

谢谢各位了!等!!!

------解决方案--------------------
Select
A.col1,
A.col2,
B.col2 As col3
From
table1 A
Left Join
table2 B
On A.col1 = B.col1
------解决方案--------------------
Select A.col1,A.col2,B.col2 As col3
From table1 A,table2 B
where A.col1 *= B.col1--用*=连接也行

------解决方案--------------------
longsql() ( ) 信誉:100 Blog 2007-03-09 16:20:59 得分: 0


我想继续向表一中插入一些符合上述条件的值该怎么做啊,表1中有些是null,但后来又找到一些符合条件的,将null替换掉


------------
能不能說詳細一點?

你的意思是不是“你用上面的語句生成了一個表,現在要對剛生成的那個表做些更新”?
------解决方案--------------------
再连
declare @table1 Table
(col1 Varchar(10),
col2 Int)
declare @table2 Table
(col1 Varchar(10),
col2 Varchar(10))
declare @table3 Table
(col1 Varchar(10),
col2 Varchar(10))
Insert @table1 Select 'a ', 1
Union All Select 'b ', 2
Union All Select 'c ', 3
Union All Select 'd ', 1
Union All Select 'e ', 3

Insert @table2 Select 'a ', 'x '
Union All Select 'b ', 'y '
Union All Select 'f ', 'm '
Union All Select 'g ', 'a '
Union All Select 'h ', '