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

按条件判断表内数据,决定是否插入。
比如有这样两个表, table1,table2
table1有两个字段                         table2有两个字段
fname   famount fname   fmoney
aa 100 aa 630
aa 200 bb 150
aa 300 cc 110
bb 200
bb 200
cc 100
cc 100
cc 100
现在要将这两个表里的数据合并到一个表上,如下格式。请问这个语句要怎么写呢?
fname   famount fmoney
aa 630
aa 100
aa 200
aa 300
bb 150
bb 200
bb 200
cc 110
cc 100
cc 100





------解决方案--------------------
order by fname,famount,fmoney desc
------解决方案--------------------
create table tableone
(
fname varchar(10),
famount varchar(10)

)

create table tabletwo
(
fname varchar(10),
fmoney varchar(10)

)

insert into tableone
select
'aa ' , '100 ' union all select
'aa ' , '200 ' union all select
'aa ' , '300 ' union all select
'bb ' , '200 ' union all select
'bb ' , '200 ' union all select
'cc ' , '100 ' union all select
'cc ' , '100 ' union all select
'cc ' , '100 '

insert into tabletwo
select
'aa ' , '630 ' union all select
'bb ' , '150 ' union all select
'cc ' , '110 '

select * from(
select fname,famount, isnull(null, ' ') fmoney
from tableone
union all
select fname,isnull(null, ' ') as famount,fmoney
from tabletwo) A
order by A.fname,fmoney desc
---------------------------------------------
fname famount fmoney
aa 630
aa 100
aa 200
aa 300
bb 150
bb 200
bb 200
cc 110
cc 100
cc 100